Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // PEG grammar for a minimal programming language
- Program
- = (_ Function / Statement _)*
- Function
- = "fun" _ id:Identifier _ "(" Parameters ")" _ (":" _ Identifier _)? _ (Statement)* _ "end"
- Parameter = Identifier _ ":" _ Identifier
- Parameters = (Parameter (_ "," _ Parameter)*)?
- Statement
- = Variable
- / Assignment
- / Repeat
- / While
- / If
- / Expression
- Variable
- = "var" _ id:Identifier _ (":" _ Identifier _)? "=" Expression
- Assignment
- = id:Identifier _ "=" _ Expression
- Repeat
- = "repeat" _ Expression _ "times" _ (Statement)* _ "end"
- While
- = "while" _ Expression _ "do" _ (Statement)* _ "end"
- If
- = "if" _ Expression _ "then" _ (Statement)* _ ("elseif" _ Expression _ "then" _ (Statement)*)* ("else" _ (Statement)*)? _ "end"
- Expression
- = head:Term tail:(_ ("+" / "-") _ Term)*
- Term
- = head:Factor tail:(_ ("*" / "/") _ Factor)*
- Factor
- = "(" _ expr:Expression _ ")"
- / Integer
- / String
- / CallOrIdentifier
- CallOrIdentifier "function call or variable name"
- = Identifier (Arguments)?
- Arguments "arguments"
- = "(" _ (Expression ( _ "," Expression)*) ")"
- Integer "integer"
- = [0-9]+
- String "string"
- = '"' chars:DoubleStringCharacter* '"' {
- return { type: "DoubleString", value: JSON.stringify(chars.join("")) };
- }
- / "'" chars:SingleStringCharacter* "'" {
- return { type: "SingleString", value: JSON.stringify(chars.join("")) };
- }
- DoubleStringCharacter
- = '\\' '"' { return '"'; }
- / !'"' SourceCharacter { return text(); }
- SingleStringCharacter
- = '\\' "'" { return "'"; }
- / !"'" SourceCharacter { return text(); }
- SourceCharacter
- = .
- Identifier "identifier"
- = !Reserved [a-zA-Z_]+
- Reserved
- = "var"
- / "fun"
- / "end"
- / "repeat"
- / "while"
- / "if"
- / "then"
- / "else"
- / "elseif"
- / "times"
- _ "whitespace"
- = [ \t\n\r]*
Add Comment
Please, Sign In to add comment