Skip to content

Latest commit

 

History

History
91 lines (88 loc) · 4.76 KB

File metadata and controls

91 lines (88 loc) · 4.76 KB

JavaPEG Reference

Grammar elements

note1: Symbol name can include hyphens (-) which are replaced by underlines (_) in the output code.
note2: Symbol name starts with dollar ($) means it's start symbol and never called in the grammar. This specification is to reduce the output code and the cache memory for packrat parsing.

  • Terminal elements
    • "string" - basic string
      e.g. "if" expression "then" statement
    • "string"i - basic string (case insensitive)
      e.g. "SELECT"i columns "FROM"i table ( "WHERE"i condition )?
    • [pattern] - character pattern, same as character class regular expression in Java
      e.g. [A-Za-z_] [0-9A-Za-z_]*
    • . (dot) - any character, not EOF
      e.g. "//" ( !"\n" . )*
  • Nonterminal elements
    • symbol - basic symbol
    • value = symbol - retrieve result
    • symbol(parameter) - call with parameter (no space between symbol and left parenthesis)
    • value = symbol(parameter) - call with parameter retrieving result
    • ~symbol - skip symbol ignoring inner action
  • Grouping
    • ( elements ) - sequence
    • ( elements / elements ... ) - ordered choice
  • Postfix modifiers
    • element ? - optional
    • ( elements / elements ... )? - optional for group
    • element * - zero or more
    • ( elements / elements ... )* - zero or more for group
    • element + - one or more
    • ( elements / elements ... )+ - one or more for group
  • Prefix modifiers
    • & element - and predicate
    • & ( elements / elements ... ) - and predicate for group
    • ! element - not predicate
    • ! ( elements / elements ... ) - not predicate for group
      note: !( a / b / c ) is same as !a !b !c and maybe the latter is better.
  • Action elements
    • image = < elements > - retrieve image string
      note: image = < a / b > occurs error, image = < ( a / b ) > is right.
    • : java_code - embedded action code
  • Comment
    • // comment - comment

Grammar structure

  • Grammar ::=
    • ( Header / Code )*
    • SymbolDefinition*
  • Header ::=
    • Command '=' ( StringParameter / IntegerParameter )* ';'
  • Code ::=
    • ':' ALineOfJavaCode
  • SymbolDefinition ::=
    • '<' Type? '>' SymbolName ( '(' Parameter ')' )?
      note: Omitting Type means this symbol has no action inside and output code can be shorten.
    • Element* ( '/' Element* )*
      note1: Block elements parted by choice operators (/) have different variable scope and the variable declaration must be put at each block elements.
      note2: If the symbol returns a value, a return statement must be put at the end of each block elements.
  • Element ::=
    • ( VariableName '=' )? SymbolName ( '(' Parameter ')' )? ( '?' / '*' / '+' )?
    • / '~' SymbolName ( '?' / '*' / '+' )?
    • / '"' String '"' ( '?' / '*' / '+' )?
    • / '[' Pattern ']' ( '?' / '*' / '+' )?
    • / '.' ( '?' / '*' / '+' )?
    • / StringVariableName '=' '<' Element* '>'
    • / ':' ALineOfJavaCode
    • / '(' Element* ( '/' Element* )* ')' ( '?' / '*' / '+' )?
    • / ( '&' / '!' ) SymbolName
    • / ( '&' / '!' ) '"' String '"'
    • / ( '&' / '!' ) '[' Pattern ']'
    • / ( '&' / '!' ) '.'
    • / ( '&' / '!' ) '(' Element* ( '/' Element* )* ')'
  • Comment ::=
    • '//' ALineOfComment

Header commands

note: Actually the header commands are implemented as methods named '$'+command and invoked by Java reflection. So, extended generator may implement new header commands likewise.

  • out = "output_file_name" ;
    • Redirect result output to a file.
  • package = "package_name" ;
    • Set package of output grammar.
  • import = "import_directive" ;
    • Create import declaration.
  • class = "class_name" ;
    • Set class name of output grammar.
  • class = "class_name" "superclass_name" ;
    • Set class name and superclass of output grammar.
    • The superclass should be a subclass of Parser class and may be a helper class or a base grammar.
  • class = "class_name" "superclass_name" "interfaces" ;
    • Set class name, superclass and interfaces of output grammar.
    • If no need for superclass, then can be omitted as empty string "".