
A
Publisher
arlez80
GDPeg
Tools
Parsing Grammar GDScript Debug Capture Syntax Expression Tree
Godot Parsing Expression Grammar for GDScript 1.3 update: * Added end of input $. 1.2 update: * Added debug @ and tree capture :> 1.1 update: * Fixed greedy(?) on text notation.
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
It is implementation of Parsing Expression Grammar.
If you want to parse GDScript on GDScript, so Check GDScript Parsers!
for Godot Engine 4
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
)
]),
funcref( 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
)
]),
funcref( 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
- 高速化
License
MIT License
Author
あるる / きのもと 結衣 @arlez80