Skip to content
John Gietzen edited this page Mar 30, 2019 · 2 revisions

JSON.peg

This example parses JSON formatted text strictly according to the spec.

// Based on the spec from https://www.json.org/
@namespace PegExamples
@using System.Linq;

any <object> = _ v:value _ EOF { v }

value <object>
    = string
    / number
    / obj
    / array
    / "true" { true }
    / "false" { false }
    / "null" { null }

obj <object> = "{" _ values:keyValuePair<0,,_ "," _> _ ("}" / #ERROR{ "Expected '}'" }) {
    values.ToDictionary(v => v.Key, v => v.Value)
}

keyValuePair <KeyValuePair<string, object>> = key:string _ ":" _ value:value {
    new KeyValuePair<string, object>(key, value)
}

array <IList<object>> = "[" _ values:value<0,,_ "," _> _ ("]" / #ERROR{ "Expected ']'" }) {
    values
}

string = "\"" chars:unicode* ("\"" / #ERROR{ "Expected '\"'" }) {
    string.Concat(chars)
}

unicode
    = c:. !{c == "\\" || c == "\"" || char.IsControl(c[0])} { c }
    / "\\" c:(
        e:["\/\\] { e } /
        "b" { "\b" } /
        "f" { "\f" } /
        "n" { "\n" } /
        "r" { "\r" } /
        "t" { "\t" } /
        "u" digits:("" [0-9A-F]i<4>) { ((char)Convert.ToInt32(digits, 16)).ToString() }
    ) { c }

number <object> = digits:("-"? ("0" / [1-9] [0-9]*) ("." [0-9]+)? ("e"i [-+]? [0-9]+)?) {
    double.Parse(digits)
}

_ -memoize = "" [ \t\r\n]*

EOF = !. / c:. #ERROR{ "Unexpected '" + c + "'" }

Math.peg

A mathematical expression evaluator. Enhanced from the readme to ignore whitespace, report errors, and memoize where needed.

@namespace PegExamples
@classname MathExpressionParser

start <double>
  = _ value:additive _ EOF { value }

additive <double> -memoize
    = left:additive _ "+" _ right:multiplicative { left + right }
    / left:additive _ "-" _ right:multiplicative { left - right }
    / multiplicative

multiplicative <double> -memoize
    = left:multiplicative _ "*" _ right:power { left * right }
    / left:multiplicative _ "/" _ right:power { left / right }
    / power

power <double>
    = left:primary _ "^" _ right:power { Math.Pow(left, right) }
    / primary

primary <double> -memoize
    = decimal
    / "-" _ primary:primary { -primary }
    / "(" _ additive:additive _ ")" { additive }

decimal <double>
    = value:([0-9]+ ("." [0-9]+)?) { double.Parse(value) }

_ = [ \t\r\n]*

EOF
  = !.
  / unexpected:. #error{ "Unexpected character '" + unexpected + "'." }

AMEX_CSV.peg

American Express CSV parser, supporting strings containing newlines and double quotes.

@namespace PegExamples

file <IList<object>>
  = l:line<1,, eol> eol eof { l }

line<object>
  = date:        bare   ","
                 string ","
    description: string ","
    cardholder:  string ","
    account:     string ","
                 string ","
                 string ","
    amount:      bare   ","
                 string ","
                 string ","
    details:     string ","
    merchant:    string ","
    address1:    string ","
    address2:    string ","
                 string ","
                 string ","
                 string
  {
    new { date, description, cardholder, account, amount, details, merchant, address1, address2 }
  }

end
  = "," / eol

bare
  = '' (!end .)*

string
  = '"' c:char* '"' { string.Concat(c) }
  / &end            { null }

char
  = c:[^"\\] { c }
  / '\\' c:. { c }

eol
  = '\r\n' / '\n'

eof
  = !.
Clone this wiki locally