Skip to main content

module lang::flybytes::demo::pico::Syntax

rascal-0.40.17
flybytes-0.2.8

Usage

import lang::flybytes::demo::pico::Syntax;

Source code

http://github.com/usethesource/flybytes/blob/main/src/lang/flybytes/demo/pico/Syntax.rsc

Dependencies

import ParseTree;

syntax Program

start syntax Program 
= program: "begin" Declarations decls {Statement ";"}* body "end" ;

syntax Declarations

syntax Declarations 
= "declare" {IdType ","}* decls ";" ;

syntax IdType

syntax IdType = idtype: Id id ":" Type t;

syntax Statement

syntax Statement 
= assign: Id var ":=" Expression val
| cond: "if" Expression cond "then" {Statement ";"}* thenPart "else" {Statement ";"}* elsePart "fi"
| cond: "if" Expression cond "then" {Statement ";"}* thenPart "fi"
| loop: "while" Expression cond "do" {Statement ";"}* body "od"
;

syntax Type

syntax Type 
= natural:"natural"
| string :"string"
| nil :"nil-type"
;

syntax Expression

syntax Expression 
= id: Id name
| strcon: String string
| natcon: Natural natcon
| bracket "(" Expression e ")"
> left concat: Expression lhs "||" Expression rhs
> left ( add: Expression lhs "+" Expression rhs
| min: Expression lhs "-" Expression rhs
)
;

syntax Id

lexical Id  = [a-z][a-z0-9]* !>> [a-z0-9];

syntax Natural

lexical Natural = [0-9]+ ;

syntax String

lexical String = "\"" ![\"]*  "\"";

syntax Layout

layout Layout = WhitespaceAndComment* !>> [\ \t\n\r%];

syntax WhitespaceAndComment

lexical WhitespaceAndComment 
= [\ \t\n\r]
| @category="Comment" "%" ![%]+ "%"
| @category="Comment" "%%" ![\n]* $
;

function program

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);
}