A
Publisher
arlez80
GDPeg for Godot Engine 4
Tools
Parsing Grammar GDScript PEG Syntax Development ParsingExpressionGrammar Code
Parsing Expression Grammar for GDScript, Godot Engine 4
This plugin has been mirrored from the Godot Asset Library.
The plugin author is in no way affiliated with Gadget.
If you are the author of this plugin and would like this mirror removed, please contact support@gadgetgodot.com.
"GDPeg" Parsing Expression Grammar for GDScript, GodotEngine 4
It is implementation of Parsing Expression Grammar.
If you want to parse GDScript on GDScript, so Check GDScript Parsers!
for Godot Engine 3
Help
How to Use
Text Notation
func number( s:String ):
return { "number": int(s) }
func binop_non_folding( group:Array ):
var node = group[0]
for i in range( 1, len( group ), 2 ):
node = { "op": group[i+0], "left": node, "right": group[i+1] }
return node
func show_tree( leaf:Dictionary ) -> String:
if leaf.has("op"):
return "(%s %s %s)" % [
leaf.op,
show_tree( leaf.left ),
show_tree( leaf.right )
]
else:
return leaf.number
func _ready( ):
var parser:GDPeg.PegTree = GDPeg.generate( """
expr String:
if leaf.has("op"):
return "(%s %s %s)" % [
leaf.op,
show_tree( leaf.left ),
show_tree( leaf.right )
]
else:
return leaf.number
func _ready( ):
var number:GDPeg.PegTree = GDPeg.capture( GDPeg.regex( "[0-9]+" ), funcref( self, "number" ) )
var term:GDPeg.PegTree = GDPeg.capture_folding(
GDPeg.concat([
number,
GDPeg.greedy(
GDPeg.capture_group(
GDPeg.concat([
GDPeg.capture(
GDPeg.select([
GDPeg.literal( "*" ),
GDPeg.literal( "/" ),
GDPeg.literal( "%" ),
])
),
number,
])
),
0
)
]),
self.binop
)
var expr:GDPeg.PegTree = GDPeg.capture_folding(
GDPeg.concat([
term,
GDPeg.greedy(
GDPeg.capture_group(
GDPeg.concat([
GDPeg.capture(
GDPeg.select([
GDPeg.literal( "+" ),
GDPeg.literal( "-" ),
])
),
term,
])
),
0
)
]),
self.binop
)
var parser:GDPeg.PegTree = expr
var result:GDPeg.PegResult = parser.parse( "1+2+3*4+5", 0 )
print( result.accept )
print( result.capture[0] )
print( show_tree( result.capture[0] ) )
TODO
- 高速化
- Lambda式使うように書き換える
License
MIT License
Author
あるる / きのもと 結衣 @arlez80