Skip to content

Instantly share code, notes, and snippets.

@fuhrmanator
Last active January 2, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fuhrmanator/82e540383fd2bba9f498975387ebb1d0 to your computer and use it in GitHub Desktop.
Save fuhrmanator/82e540383fd2bba9f498975387ebb1d0 to your computer and use it in GitHub Desktop.
// Quick-and-dirty PEG.js grammar from EBNF published in https://arxiv.org/abs/2011.10975
// Try it with https://pegjs.org/online
// see github:profcfuhrmanets/mse-tools for latest
Root = d:Document ? {return {nodes:d}}
Document = OPEN nodes:(ElementNode *) CLOSE {return nodes}
ElementNode = OPEN name:ELEMENTNAME id:Serial ? attrNodes:(AttributeNode *) CLOSE
{return {name:name, id:id, attrs:attrNodes}}
Serial = OPEN ID id:INTEGER CLOSE {return id}
AttributeNode = OPEN n:SIMPLENAME vals:(ValueNode *) CLOSE {return {name:n, vals:vals } }
ValueNode = Primitive / Reference / ElementNode
Primitive = STRING / NUMBER / Boolean
Boolean = TRUE / FALSE
Reference = IntegerReference / NameReference
IntegerReference = OPEN REF i:INTEGER CLOSE {return {ref:i}}
NameReference = OPEN REF e:ELEMENTNAME CLOSE {return {ref:e}}
OPEN = _ "(" _
CLOSE = _ ")" _
ID = _ t:"id:" _ {return t}
REF = _ t:"ref:" _ {return t}
TRUE = _ t:"true" _ {return t}
FALSE = _ t:"false" _ {return t}
ELEMENTNAME = _ letter ( letter / digit ) * ( "." letter ( letter / digit ) * ) _ {return text().trim()}
SIMPLENAME = _ letter ( letter / digit ) * _ {return text().trim()}
INTEGER = _ digit + _ {return text().trim()}
NUMBER = _ "-" ? digit + ( "." digit + ) ? ( ( "e" / "E" ) ( "-" / "+" ) ? digit + ) ? _ {return text().trim()}
STRING = _ ( "\'" [^'] * "\'" ) + _ {return text().trim()}
letter = [A-Za-z]
digit = [0-9]
_ = space*
space = [ \t\n\r]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment