Last active
November 24, 2015 19:53
-
-
Save forestbelton/d2c1c743d523fc998935 to your computer and use it in GitHub Desktop.
data declaration peg parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Decl "data declaration" | |
= Data name:TName params:TParam* Equals first:Constructor rest:Alternative* { | |
return { | |
name: name, | |
params: params, | |
constructors: [first].concat(rest) | |
}; | |
} | |
Data "data keyword" | |
= "data" _ | |
TName "type name" | |
= first:[A-Z] rest:[A-Za-z_0-9]* _ { | |
return first + rest.join(''); | |
} | |
TParam "type parameter" | |
= first:[a-z] rest:[A-Za-z_0-9]* _ { | |
return first + rest.join(''); | |
} | |
Equals "=" | |
= "=" _ | |
Constructor "constructor" | |
= name:TName params:ConstrArg* { | |
return { | |
name: name, | |
params: params | |
}; | |
} | |
Alternative "alternative" | |
= Or cons:Constructor { | |
return cons; | |
} | |
ConstrArg | |
= name:TName { | |
return { | |
type: "Concrete", | |
value: name | |
}; | |
} | |
/ vari:TParam { | |
return { | |
type: "Variable", | |
name: vari | |
}; | |
} | |
/ LParen head:ConstrArg tail:ConstrArg* RParen { | |
return { | |
type: "Application", | |
head: head, | |
tail: tail | |
}; | |
} | |
LParen "(" | |
= "(" _ | |
RParen ")" | |
= ")" _ | |
Or "|" | |
= "|" _ | |
_ "whitespace" | |
= [ \t\r\n]* |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment