57 lines
1.5 KiB
Plaintext
57 lines
1.5 KiB
Plaintext
module lang::pico::Syntax
|
|
|
|
import Prelude;
|
|
|
|
lexical Id = [a-z][a-z0-9]* !>> [a-z0-9];
|
|
lexical Natural = [0-9]+ ;
|
|
lexical String = "\"" ![\"]* "\"";
|
|
|
|
layout Layout = WhitespaceAndComment* !>> [\ \t\n\r%];
|
|
|
|
lexical WhitespaceAndComment
|
|
= [\ \t\n\r]
|
|
| @category="Comment" "%" ![%]+ "%"
|
|
| @category="Comment" "%%" ![\n]* $
|
|
;
|
|
|
|
start syntax Program
|
|
= program: "begin" Declarations decls {Statement ";"}* body "end" ;
|
|
|
|
syntax Declarations
|
|
= "declare" {Declaration ","}* decls ";" ;
|
|
|
|
syntax Declaration = decl: Id id ":" Type tp;
|
|
|
|
syntax Type
|
|
= natural:"natural"
|
|
| string :"string"
|
|
;
|
|
|
|
syntax Statement
|
|
= asgStat: Id var ":=" Expression val
|
|
| ifElseStat: "if" Expression cond "then" {Statement ";"}* thenPart "else" {Statement ";"}* elsePart "fi"
|
|
| ifThenStat: "if" Expression cond "then" {Statement ";"}* thenPart "fi"
|
|
| whileStat: "while" Expression cond "do" {Statement ";"}* body "od"
|
|
| doUntilStat: "do" {Statement ";"}* body "until" Expression cond "od"
|
|
| unlessStat: Statement "unless" Expression cond
|
|
;
|
|
|
|
syntax Expression
|
|
= id: Id name
|
|
| strCon: String string
|
|
| natCon: Natural natcon
|
|
| bracket "(" Expression e ")"
|
|
> left conc: Expression lhs "||" Expression rhs
|
|
> left ( add: Expression lhs "+" Expression rhs
|
|
| sub: Expression lhs "-" Expression rhs
|
|
)
|
|
;
|
|
|
|
public start[Program] program(str s) {
|
|
return parse(#start[Program], s);
|
|
}
|
|
|
|
public start[Program] program(str s, loc l) {
|
|
return parse(#start[Program], s, l);
|
|
}
|