module examples::pascal::Syntax
rascal-0.40.16
typepal-0.14.8
Usage
import examples::pascal::Syntax;
Source code
http://github.com/usethesource/typepal/src/examples/pascal/Syntax.rsc
syntax Layout
layout Layout = WhitespaceAndComment* !>> [\ \t\n\r%];
syntax WhitespaceAndComment
lexical WhitespaceAndComment
= [\ \t\n\r]
| @category="Comment" ws2:
"%" ![%]+ "%"
| @category="Comment" ws3: "{" ![}]* "}"$
;
syntax Program
start syntax Program =
ProgramHeading programHeading Block block ".";
syntax ProgramHeading
syntax ProgramHeading =
"program" Identifier id "(" {FileIdentifier ","}+ fileIdentifiers ")" ";";
syntax FileIdentifier
syntax FileIdentifier
= Identifier
;
syntax Identifier
lexical Identifier
= ([A-Za-z] [A-Za-z0-9]* !>> [A-Za-z0-9]) \ Reserved
;
syntax Reserved
keyword Reserved
= "and" | "array" | "begin" | "case" | "const" | "div" | "do" | "downto" | "else"
| "end" | "file" | "for" | "function" | "goto" | "if" | "in" | "label" | "mod"
| "nil" | "not" | "of" | "or" | "packed" | "procedure" | "program" | "record" | "repeat"
| "set" | "then" | "to" | "type" | "until" | "var" | "while" | "with"
;
syntax Block
syntax Block
= LabelDeclarationPart? ConstantDefinitionPart? TypeDefinitionPart?
VariableDeclarationPart? ProcedureAndFunctionDeclarationPart? StatementPart
;
syntax LabelDeclarationPart
syntax LabelDeclarationPart
= "label" {Label ","}+ labels ";"
;
syntax Label
syntax Label
= UnsignedInteger
;
syntax ConstantDefinitionPart
syntax ConstantDefinitionPart
= "const" {ConstantDefinition ";"}+ ";"
;
syntax ConstantDefinition
syntax ConstantDefinition
= Identifier id "=" Constant constant
;
syntax Constant
syntax Constant
= unumber: UnsignedNumber unsignedNumber
| signednumber: Sign sign UnsignedNumber unsignedNumber
| constantid: ConstantIdentifier constantIdentifier
| signedconstant: Sign sign ConstantIdentifier constantIdentifier
| string: String string
;
syntax UnsignedNumber
lexical UnsignedNumber
= integer: UnsignedInteger
| \real: UnsignedReal
;
syntax UnsignedInteger
lexical UnsignedInteger
= [0-9]+ !>> [0-9]
;
syntax UnsignedReal
lexical UnsignedReal
= UnsignedInteger "." UnsignedInteger
| UnsignedInteger "." UnsignedInteger "e" ScaleFactor
| UnsignedInteger "e" ScaleFactor
;
syntax ScaleFactor
lexical ScaleFactor
= UnsignedInteger
| Sign UnsignedInteger
;
syntax Sign
lexical Sign = "+" | "-";
syntax ConstantIdentifier
syntax ConstantIdentifier
= Identifier
;
syntax String
lexical String
= "\'" Character* "\'"
;
syntax Character
lexical Character
= ![\']
| "\'\'"
;
syntax TypeDefinitionPart
syntax TypeDefinitionPart
= "type" {TypeDefinition ";"}+ ";"
;
syntax TypeDefinition
syntax TypeDefinition
= Identifier id "=" Type rtype
;
syntax Type
syntax Type
= simple: SimpleType simpleType
| structured: StructuredType structuredType
| pointer: "^" TypeIdentifier typeIdentifier
;
syntax SimpleType
syntax SimpleType
= scalar: ScalarType scalarType
| subrange: SubrangeType subrangeType
| typeid: TypeIdentifier typeIdentifier
;
syntax ScalarType
syntax ScalarType
= "(" {Identifier ","}+ ids ")"
;
syntax SubrangeType
syntax SubrangeType
= Constant from ".." Constant to
;
syntax TypeIdentifier
syntax TypeIdentifier
= Identifier
;
syntax StructuredType
syntax StructuredType
= unpacked: UnpackedStructuredType unpackedStructuredType
| packed: "packed" UnpackedStructuredType unpackedStructuredType
;
syntax UnpackedStructuredType
syntax UnpackedStructuredType
= array: ArrayType arrayType
| record: RecordType recordType
| \set: SetType setType
| file: FileType fileType
;
syntax ArrayType
syntax ArrayType
= "array" "[" {SimpleType ","}+ indexTypes "]" "of" Type rtype
;
syntax RecordType
syntax RecordType
= "record" FieldList fieldList "end"
;
syntax FieldList
syntax FieldList
= FixedPart fixedPart
| FixedPart fixedPart ";" VariantPart variantPart
| VariantPart variantPart
;
syntax FixedPart
syntax FixedPart
= {RecordSection ";"}+ recordSections
;
syntax RecordSection
syntax RecordSection
= {FieldIdentifier ","}+ fieldIdentifiers ":" Type rtype
| ()
;
syntax VariantPart
syntax VariantPart
= "case" TagField TypeIdentifier "of" {Variant ";"}+ variantList
;
syntax TagField
syntax TagField
= FieldIdentifier identifier ":"
| ()
;
syntax Variant
syntax Variant
= {CaseLabel ","}+ ":" "(" FieldList ")"
| ()
;
syntax CaseLabel
syntax CaseLabel
= Constant
;
syntax SetType
syntax SetType
= "set" "of" SimpleType simpleType
;
syntax FileType
syntax FileType
= "file" "of" Type type
;
syntax PointerType
syntax PointerType
= "^" TypeIdentifier
;
syntax VariableDeclarationPart
syntax VariableDeclarationPart
= "var" {VariableDeclaration ";"}+ ";"
;
syntax VariableDeclaration
syntax VariableDeclaration
= {Identifier ","}+ ids ":" Type type
;
syntax ProcedureAndFunctionDeclarationPart
syntax ProcedureAndFunctionDeclarationPart
= ProcedureOrFunctionDeclaration+
;
syntax ProcedureOrFunctionDeclaration
syntax ProcedureOrFunctionDeclaration
= ProcedureDeclaration ";"
| FunctionDeclaration ";"
;
syntax ProcedureDeclaration
syntax ProcedureDeclaration
= ProcedureHeading procedureHeading Block block
;
syntax ProcedureHeading
syntax ProcedureHeading
= "procedure" Identifier id ";"
| "procedure" Identifier id "(" {FormalParameterSection ";"}+ formals ")" ";"
;
syntax FormalParameterSection
syntax FormalParameterSection
= ParameterGroup group
| "var" ParameterGroup group
| "function" ParameterGroup group
| "procedure" {Identifier ","}+
;
syntax ParameterGroup
syntax ParameterGroup
= {Identifier ","}+ ids ":" Type rtype
;
syntax FunctionDeclaration
syntax FunctionDeclaration
= FunctionHeading functionHeading Block block
;
syntax FunctionHeading
syntax FunctionHeading
= "function" Identifier id ":" ResultType rtype ";"
| "function" Identifier id "(" {FormalParameterSection ";"}+ formals ")" ":" ResultType rtype ";"
;
syntax ResultType
syntax ResultType
= TypeIdentifier typeIdentifier
;
syntax StatementPart
syntax StatementPart
= CompoundStatement
;
syntax Statement
syntax Statement
= UnlabelledStatement unlabelledStatement
| Label label ":" UnlabelledStatement unlabelledStatement
;
syntax UnlabelledStatement
syntax UnlabelledStatement
= SimpleStatement
| StructuredStatement
;
syntax SimpleStatement
syntax SimpleStatement
= AssignmentStatement
| ProcedureStatement
| GoToStatement
| EmptyStatement
;
syntax AssignmentStatement
syntax AssignmentStatement
= Variable ":=" Expression
// | FunctionIdentifier ":=" Expression
;
syntax Variable
syntax Variable
= entire: EntireVariable
| component: ComponentVariable
| referenced: ReferencedVariable
;
syntax EntireVariable
syntax EntireVariable
= VariableIdentifier
;
syntax VariableIdentifier
syntax VariableIdentifier
= Identifier
;
syntax ComponentVariable
syntax ComponentVariable
= IndexedVariable
| FieldDesignator
| FileBuffer
;
syntax IndexedVariable
syntax IndexedVariable
= ArrayVariable "[" { Expression ","}+ "]"
;
syntax ArrayVariable
syntax ArrayVariable
= Variable
;
syntax FieldDesignator
syntax FieldDesignator
= RecordVariable "." FieldIdentifier
;
syntax RecordVariable
syntax RecordVariable
= Variable variable
;
syntax FieldIdentifier
syntax FieldIdentifier
= Identifier
;
syntax FileBuffer
syntax FileBuffer
= FileVariable "^" !>> "."
;
syntax FileVariable
syntax FileVariable
= Variable
;
syntax ReferencedVariable
syntax ReferencedVariable
= PointerVariable "^"
;
syntax PointerVariable
syntax PointerVariable
= Variable
;
syntax Expression
syntax Expression
=
FunctionDesignator
> Variable
| UnsignedConstant
| "(" Expression ")"
| Set
> left ( Expression "*" Expression
| Expression "/" Expression
| Expression "div" Expression
| Expression "mod" Expression
| Expression "and" Expression
)
> left ( Expression "+" Expression
| Expression "-" Expression
| Expression "or" Expression
)
| left ( Expression "=" Expression
| Expression "\<\>" Expression
| Expression "\<" Expression
| Expression "\<=" Expression
| Expression "\>=" Expression
| Expression "\>" Expression
| Expression "in" Expression
)
> non-assoc "not" Expression
> non-assoc Sign Expression
;
syntax UnsignedConstant
syntax UnsignedConstant
= unumber: UnsignedNumber unsignedNumber
| string: String string
//| ConstantIdentifier
| nil: "nil"
;
syntax FunctionDesignator
syntax FunctionDesignator
= //FunctionIdentifier |
FunctionIdentifier "(" { ActualParameter ","}+ ")"
;
syntax FunctionIdentifier
syntax FunctionIdentifier
= Identifier
;
syntax Set
syntax Set
= "[" { Element ","}* "]"
;
syntax Element
syntax Element
= Expression
| Expression ".." Expression
;
syntax ProcedureStatement
syntax ProcedureStatement
= ProcedureIdentifier
| ProcedureIdentifier "(" {ActualParameter ","}+ ")"
;
syntax ProcedureIdentifier
syntax ProcedureIdentifier
= Identifier
;
syntax ActualParameter
syntax ActualParameter
= Expression
// | Variable
// | ProcedureIdentifier
// | FunctionIdentifier
;
syntax GoToStatement
syntax GoToStatement
= "goto" Label label
;
syntax EmptyStatement
syntax EmptyStatement
= ()
;
syntax StructuredStatement
syntax StructuredStatement
= CompoundStatement
| ConditionalStatement
| RepetitiveStatement
| WithStatement
;
syntax CompoundStatement
syntax CompoundStatement
= "begin" { Statement ";" }+ "end"
;
syntax ConditionalStatement
syntax ConditionalStatement
= IfStatement
| CaseStatement
;
syntax IfStatement
syntax IfStatement
= "if" Expression condition "then" Statement () !>> "else"
| "if" Expression condition "then" Statement "else" Statement
;
syntax CaseStatement
syntax CaseStatement
= "case" Expression "of" {CaseListElement ";"}+ "end"
;
syntax CaseListElement
syntax CaseListElement
= CaseLabelList ":" Statement
| ()
;
syntax CaseLabelList
syntax CaseLabelList
= { CaseLabel ","}+
;
syntax RepetitiveStatement
syntax RepetitiveStatement
= WhileStatement
| RepeatStatement
| ForStatement
;
syntax WhileStatement
syntax WhileStatement
= "while" Expression condition "do" Statement
;
syntax RepeatStatement
syntax RepeatStatement
= "repeat" {Statement ";"}+ "until" Expression condition
;
syntax ForStatement
syntax ForStatement
= "for" Identifier control ":=" ForList forList "do" Statement
;
syntax ForList
syntax ForList
= Expression initial "to" Expression final
| Expression initial "downto" Expression final
;
syntax WithStatement
syntax WithStatement
= "with" {RecordVariable ","}+ "do" Statement
;