Giter Site home page Giter Site logo

gdpeg4's Introduction

"GDPeg" Parsing Expression Grammar for GDScript, GodotEngine 4

It is implementation of Parsing Expression Grammar.

ko-fi

If you want to parse GDScript on GDScript, so Check GDScript Parsers!

for Godot Engine 3

See this repository

Help

Document

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 < term ( ~\"[+\\-]\" term )* $
		term < factor ( ~\"[*/]\" factor )*
		factor < number / \"(\" expr \")\"
		number <~ ~\"[0-9]+\"
	""", {
		"expr": self.binop_non_folding
	,	"term": self.binop_non_folding
	,	"number": self.number
	} )
	var result:GDPeg.PegResult = parser.parse( "1+2+3*4+5", 0 )

	print( result.accept )
	if result.accept:
		print( result.capture[0] )
		print( show_tree( result.capture[0] ) )

Instance Notation

結構柔軟に書ける。

func number( s:String ):
	return { "number": int(s) }

func binop( root:Array, group:Array ):
	return { "op": group[0], "left": root[0], "right": group[1] }

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 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

gdpeg4's People

Contributors

arlez80 avatar jadoc avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.