DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

IoPeg

06.25.2008
| 1972 views |
  • submit to reddit
        This is a direct port of the IoPeg metagrammar (rev 46) as found here: http://iopeg.googlecode.com/svn/trunk/PEG.peg

#  Hierarchical syntax
grammar IoPeg

  rule grammar
    spacing definition+ end_of_file
  end

  rule definition
    identifier leftarrow expression
  end

  rule expression
    sequence (slash sequence)*
  end

  rule sequence
    prefix*
  end

  rule prefix
    (and / not)? suffix
  end

  rule suffix
    primary (question / star / plus)?
  end

  rule primary
    identifier !leftarrow / open expression close / literal / class / dot
  end

  rule identifier
    ident_start ident_cont* spacing
  end

  rule ident_start
    [a-zA-Z_]
  end

  rule ident_cont
    ident_start / [0-9]
  end

  rule literal
    ['] (!['] char)* ['] spacing / ["] (!["] char)* ["] spacing
  end

  rule class
    '[' (!']' range)* ']' spacing
  end

  rule range
    char '-' char / char
  end

  rule char
    "\\" [nrt'"\[\]\\] / "\\" [0-2] [0-7] [0-7] / "\\" [0-7] [0-7]? / !"\\" .
  end

  rule leftarrow
    '<-' spacing
  end

  rule slash
    '/' spacing
  end

  rule and
    '&' spacing
  end

  rule not
    '!' spacing
  end

  rule question
    '?' spacing
  end

  rule star
    '*' spacing
  end

  rule plus
    '+' spacing
  end

  rule open
    '(' spacing
  end

  rule close
    ')' spacing
  end

  rule dot
    '.' spacing
  end

  rule spacing
    (space / comment)*
  end

  rule comment
    '#' (!end_of_line .)* end_of_line
  end

  rule space
    ' ' / "\t" / end_of_line
  end

  rule end_of_line
    "\r\n" / "\n" / "\r"
  end

  rule end_of_file
    !.
  end

end