Modula-2 Reloaded

A Modern Typesafe & Literate Programming Notation

Site Menu

Project

Specification

Implementation

Recommendations

Reference

Needs Updating

Work in Progress

Wastebasket

Wiki Manual

edit SideBar

Non Terminals

<< Grammar | Terminals >>

Index

#1 Compilation Unit   syntax diagram
compilationUnit :=
  definitionModule | implOrPrgmModule | blueprint
  ;

Definition Module Syntax

#2 Definition Module   syntax diagram
definitionModule :=
  DEFINITION MODULE moduleIdent
  ( '[' blueprintToObey ']' )? ( FOR typeToExtend )? ';'
  ( import ';' )* definition*
  END moduleIdent '.'
  ;

#2.1 Module Identifier, Blueprint Identifier, Type To Extend   syntax diagram
moduleIdent := Ident ;

blueprintIdent := Ident ;

typeToExtend := Ident ;

#2.2 Blueprint To Obey   syntax diagram
blueprintToObey := blueprintIdent ;

#3 Import   syntax diagram
import :=
  IMPORT impexLib ( ',' moreImpexLibs | ALIAS unqualifiedAliases )? |
  libGenDirective
  ;

#3.1 Import/Re-Export Library   syntax diagram
impexLib :=
  Ident reExport?
  ;

#3.2 More Import/Re-Export Libraries   syntax diagram
moreImpexLibs :=
  impexLib ( ',' impexLib )*
  ;

#3.3 Re-Export Tag   syntax diagram
reExport := '+' ;

#3.4 Unqualified Aliases   syntax diagram
unqualifiedAliases := identList ;

#4 Library Generation Directive   syntax diagram
libGenDirective :=
  GENLIB libIdent FROM template FOR templateParamList END
  ;

#4.1 Library Identifier, Template, Placeholder   syntax diagram
libIdent := Ident ;

template := Ident ;

placeholder := Ident ;

#4.2 Replacement   syntax diagram
replacement :=
  NumberLiteral | StringLiteral | ChevronText
  ;

#5 Identifier List   syntax diagram
identList :=
    Ident ( ',' Ident )*
    ;

#6 Definition   syntax diagram
definition :=
  CONST ( constDefinition ';' )+ |
  TYPE ( typeDefinition ';' )+ |
  VAR ( identList '=' typeIdent ';' )+ |
  procedureHeader ';'
  ;

#6.1 Type Identifier   syntax diagram
typeIdent := qualident ;

#7 Constant Definition   syntax diagram
constDefinition :=
  ( '[' propertyToBindTo ']' | restrictedExport )?
  Ident '=' constExpression
  ;

#7.1 Constant Expression   syntax diagram
constExpression := expression ;

#7.2 Restricted Export   syntax diagram
restrictedExport := '*' ;

#8 Type Definition   syntax diagram
typeDefinition :=
  restrictedExport? Ident '=' ( type | OPAQUE )
  ;

#9 Qualified Identifier   syntax diagram
qualident :=
  Ident ( '.' Ident )*
  ;

#10 Type   syntax diagram
type :=
  derivedType | equivType | subrangeType | enumType | setType |
  arrayType | recordType | pointerType | coroutineType | procedureType
  ;

#10.1 Derived Type   syntax diagram
derivedType := typeIdent ;

#10.2 Equivalence Type   syntax diagram
equivType :=
  ALIAS OF typeIdent | CONST dynamicTypeIdent
  ;

#10.3 Dynamic Type Identifier   syntax diagram
dynamicTypeIdent := typeIdent ;

#11 Subrange Type   syntax diagram
subrangeType :=
  range OF ordinalOrScalarType
  ;

#11.1 Range   syntax diagram
range :=
  '[' greaterThan? constExpression '..' lessThan? constExpression ']'
  ;

#11.2 Greater Than   syntax diagram
greaterThan := '>' ;

#11.3 Less Than   syntax diagram
lessThan := '<' ;

#11.4 Ordinal Or Scalar Type   syntax diagram
ordinalOrScalarType := typeIdent ;

#12 Enumeration Type   syntax diagram
enumType :=
  '(' ( '+' enumTypeToExtend )? identList ')'
  ;

#12.1 Enumeration Type To Extend   syntax diagram
enumTypeToExtend := typeIdent ;

#12.2 Enumeration Type Identifier   syntax diagram
enumTypeIdent := typeIdent ;

#13 Set Type   syntax diagram
setType :=
  SET OF enumTypeIdent
  ;

#14 Array Type   syntax diagram
arrayType :=
  ARRAY lessThan? valueCount ( ',' valueCount )* OF typeIdent
  ;

#14.1 Value Count   syntax diagram
valueCount := constExpression ;

#15 Record Type   syntax diagram
recordType :=
  RECORD ( '(' recTypeToExtend ')' )? fieldList ( ';' fieldList )* )
  ;

#15.1 Field List   syntax diagram
fieldList :
  restrictedExport? variableDeclaration ( '=' constExpression )?
  ;

#15.2 Record Type To Extend   syntax diagram
recTypeToExtend := typeIdent ;

#16 Pointer Type   syntax diagram
pointerType :=
  POINTER TO CONST? typeIdent
  ;

#17 Coroutine Type   syntax diagram
coroutineType :=
  COROUTINE '(' assocProcType ')'
  ;

#17.1 Associated Procedure Type   syntax diagram
assocProcType : typeIdent ;

#18 Procedure Type   syntax diagram
procedureType :=
  PROCEDURE ( formalType ( ',' formalType )* )? ( ':' returnedType )?
  ;

#18.1 Formal Type   syntax diagram
formalType :=
  simpleFormalType | attributedFormalType | variadicFormalType
  ;

#19 Simple Formal Type   syntax diagram
simpleFormalType :=
  ( ARRAY OF )? typeIdent | castingFormalType
  ;

#19.1 Casting Formal Type   syntax diagram
castingFormalType :=
  CAST ( ARRAY OF OCTET | addressTypeIdent )
  ;

#19.2 Address Type Identifier   syntax diagram
addressTypeIdent :=
  ( UNSAFE '.' )? ADDRESS
  ;

#20 Attributed Formal Type   syntax diagram
attributedFormalType :=
  ( CONST | NEW | VAR ) ( simpleFormalType | simpleVariadicFormalType )
  ;

#21 Simple Variadic Formal Type   syntax diagram
simpleVariadicFormalType :=
  ARGLIST reqNumOfArgs? OF simpleFormalType terminator?
  ;

#21.1 Required Number Of Arguments   syntax diagram
reqNumOfArgs :=
  greaterThan? constExpression
  ;

#21.2 Argument List Terminator   syntax diagram
terminator :=
  '|' constQualident
  ;

#21.3 Constant Qualified Identifier   syntax diagram
constQualident := qualident ;

#22 Variadic Formal Type   syntax diagram
variadicFormalType :=
  ARGLIST reqNumOfArgs? OF
    ( '{' nonVariadicFormalType ( ';' nonVariadicFormalType )* '}' |
      simpleFormalType ) terminator?
  ;

#23 Non-Variadic Formal Type   syntax diagram
nonVariadicFormalType :=
  ( CONST | NEW | VAR )? simpleFormalType
  ;

#24 Procedure Header   syntax diagram
procedureHeader :=
  PROCEDURE ( '[' ( entityToBindTo | COROUTINE ) ']' | restrictedExport )?
  procedureSignature
  ;

#25 Procedure Signature   syntax diagram
procedureSignature :=
  Ident ( '(' formalParams ( ';' formalParams )* ')' )? ( ':' returnedType )?
  ;

#26 Formal Parameters   syntax diagram
formalParams :=
  identList ':' ( simpleFormalType | variadicFormalParams ) |
  attributedFormalParams
  ;

#27 Attributed Formal Parameters   syntax diagram
attributedFormalParams :=
  ( CONST | NEW | VAR ) identList ':'
  ( simpleFormalType | simpleVariadicFormalType )
  ;

#28 Variadic Formal Parameters   syntax diagram
variadicFormalParams :=
  ARGLIST reqNumOfArgs? OF
    ( ( '{' nonVariadicFormalParams ( ';' nonVariadicFormalParams )* '}') |
      simpleFormalType ) terminator?
  ;

#29 Non-Variadic Formal Parameters   syntax diagram
nonVariadicFormalParams :=
  ( CONST | NEW | VAR )? identList ':' simpleFormalType
  ;

Implementation And Program Module Syntax

#30 Implementation Or Program Module   syntax diagram
implOrPrgmModule :=
  IMPLEMENTATION? MODULE moduleIdent ';'
  ( privateImport ';' )* block moduleIdent '.'
  ;

#31 Private Import   syntax diagram
privateImport :=
  IMPORT importLib ( ',' moreImportLibs | ALIAS privateAliases )?
  ;

#31.1 Import Library   syntax diagram
importLib := Ident ;

#31.2 More Import Libraries   syntax diagram
moreImportLibs := identList ;

#31.3 Private Aliases   syntax diagram
privateAliases := identList | '*' ;

#32 Block   syntax diagram
block :=
  declaration* ( BEGIN statementSequence )? END
  ;

#33 Declaration   syntax diagram
declaration :=
  CONST ( Ident '=' constExpression ';' )+ |
  TYPE ( typeDeclaration ';' )+ |
  VAR ( variableDeclaration ';' )+ |
  ALIAS localAliases FOR origin ';'
  procedureHeader ';' block Ident ';'
  ;

#33.1 Local Aliases   syntax diagram
localAliases := privateAliases ;

#33.2 Origin   syntax diagram
origin := ( Ident '.' )+ '*' ;

#34 Type Declaration   syntax diagram
typeDeclaration :=
  Ident '=' ( type | indeterminateRecType )
  ;

#34.1 Indeterminate Record Type   syntax diagram
indeterminateRecType :=
  '~' RECORD ( variableDeclaration ';' )+ indeterminateField END
  ;

#34.2 Indeterminate Field   syntax diagram
indeterminateRecType :=
  '~' Ident ':' ARRAY '~' discriminantFieldIdent OF typeIdent END
  ;

#34.3 Discriminant Field Identifier   syntax diagram
discriminantFieldIdent := Ident ;

#35 Variable Declaration   syntax diagram
variableDeclaration :=
  identList ':' ( ( ARRAY valueCount OF )? typeIdent | subrangeType )
  ;

#36 Statement Sequence   syntax diagram
statementSequence :=
  statement ( ';' statement )*
  ;

#37 Statement   syntax diagram
statement :=
  memMgtOperation | updateOrProcCall | ifStatement | caseStatement |
  loopStatement | whileStatement | repeatStatement | forStatement | EXIT
  ;

#38 Memory Management Operation   syntax diagram
memMgtOperation :=
  NEW designator ( OF initSize | := initValue )? |
  RETAIN designator |
  RELEASE designator
  ;

#38.1 Initialisation Size, Initialisation Value   syntax diagram
initSize := expression ;

initValue := expression ;

#39 Update Or Procedure Call   syntax diagram
updateOrProcCall :=
  designator ( incOrDecSuffix | ':=' expression | '(' expressionList ')' )? |
  COPY designator ':=' expression
  ;

#39.1 Increment Or Decrement Suffix   syntax diagram
incOrDecSuffix :=
  '++' | '--'
  ;

#40 Return Or Yield Statement   syntax diagram
returnStatement :=
  ( RETURN | YIELD ) expression?
  ;

#41 IF Statement   syntax diagram
ifStatement :=
  IF boolExpression THEN statementSequence
  ( ELSIF boolExpression THEN statementSequence )?
  ( ELSE statementSequence )?
  END
  ;

#41.1 Boolean Expression   syntax diagram
boolExpression := expression ;

#42 CASE Statement   syntax diagram
caseStatement :=
  CASE expression OF ( '|' case  )+ ( ELSE statementSequence )? END
  ;

#42.1 Case   syntax diagram
case :=
  caseLabels ( ',' caseLabels )* ':' statementSequence
  ;

#42.2 Case Labels   syntax diagram
caseLabels :=
  constExpression ( '..' constExpression )?
  ;

#43 LOOP Statement   syntax diagram
loopStatement :=
  LOOP statementSequence END
  ;

#44 WHILE Statement   syntax diagram
whileStatement :=
  WHILE boolExpression DO statementSequence END
  ;

#45 REPEAT Statement   syntax diagram
repeatStatement :=
  REPEAT statementSequence UNTIL boolExpression
  ;

#46 FOR Statement   syntax diagram
forStatement :=
  FOR forLoopVariants IN iterableExpr DO statementSequence END
  ;

#46.1 FOR Loop Variants   syntax diagram
forLoopVariants :=
  accessor ascOrDesc? ( ',' value )? |
  VALUE value ascOrDesc?
  ;

#46.2 Accessor, Value   syntax diagram
accessor := Ident ;

value := Ident ;

#46.3 Iterable Expression   syntax diagram
iterableExpr :=
  designator | '[' expression '..' expression ']' OF ordinalType
  ;

#46.4 Ascender Or Descender   syntax diagram
ascOrDesc := incOrDecSuffix ;

#46.5 Ordinal Type   syntax diagram
ordinalType := typeIdent ;

#47 Designator   syntax diagram
designator :=
  qualident designatorTail?
  ;

#47.1 Designator Tail   syntax diagram
designatorTail :=
  ( ( '[' exprListOrSlice ']' | '^' ) ( '.' Ident )* )+
  ;

#47.2 Expression List Or Slice   syntax diagram
exprListOrSlice :=
  expression ( ( ',' expression )* | '..' expression? )
  ;

#48 Expression List   syntax diagram
expressionList :=
  expression ( ',' expression )*
  ;

#49 Expression   syntax diagram
expression :=
  simpleExpression ( operL1 simpleExpression )?
  ;

#49.1 Level-1 Operator   syntax diagram
operL1 :=
  '=' | '#' | '<' | '<=' | '>' | '>=' | IN | identityOp
  ;

#49.2 Identity Operator   syntax diagram
identityOp := '==' ;

#50 Simple Expression   syntax diagram
simpleExpression :=
  term ( operL2 term )* | '-' simpleFactor
  ;

#50.1 Level-2 Operator   syntax diagram
operL2 :=
  '+' | '-' | OR | concatOp
  ;

#49.2 Concatenation Operator   syntax diagram
concatOp := '&' ;

#51 Term   syntax diagram
term :=
  simpleTerm ( operL3 simpleTerm )*
  ;

#51.1 Level-3 Operator   syntax diagram
operL3 :=
  '*' | '/' | DIV | MOD | AND | setDiffOp
  ;

#51.2 Set Difference Operator   syntax diagram
setDiffOp := '\' ;

#52 Simple Term   syntax diagram
simpleTerm :=
  NOT? factorOrTypeConv
  ;

#53 Factor   syntax diagram
factor :=
  simpleFactor ( typeConvOp typeIdent )?
  ;

#53.1 Type Conversion Operator   syntax diagram
typeConvOp := '::' ;

#54 Simple Factor   syntax diagram
simpleFactor :=
  NumberLiteral | StringLiteral | structuredValue |
  designatorOrFuncCall | '(' expression ')'
  ;

#54.1 Designator Or Function Call   syntax diagram
designatorOrFuncCall :=
  designator ( '(' expressionList? ')' )?
  ;

#54.2 Structured Value   syntax diagram
structuredValue :=
  '{' valueComponent ( ',' valueComponent )* '}'
  ;

#54.3 Value Component   syntax diagram
valueComponent :=
  constExpression (( BY | '..' )? constExpression )? | runtimeExpression
  ;

#54.4 Runtime Expression   syntax diagram
runtimeExpression := expression ;

Blueprint Syntax

#55 Blueprint   syntax diagram
blueprint :=
  BLUEPRINT blueprintIdent ( '[' blueprintToRefine ']' )?
  ( FOR blueprintForTypeToExtend )? ';' ( REFERENTIAL identList ';' )?
  MODULE TYPE '=' ( typeClassification ( ';' literalCompatibility)? | NONE ) ';'
  ( constraint ';' )* ( requirement ';' )* END blueprintIdent '.'
  ;

#55.1 Blueprint Identifier   syntax diagram
blueprintIdent := Ident ;

#55.2 Blueprint To Refine, Blueprint For Type To Extend   syntax diagram
blueprintToRefine := blueprintIdent ;

blueprintForTypeToExtend := blueprintIdent ;

#56 Type Classification   syntax diagram
typeClassification :=
  '{' determinedClassification ( ';' refinableClassification )? ( ';' '*' )? '}'
  | '*'
  ;

#56.1 Determined Classification   syntax diagram
determinedClassification :=
  classificationIdent ( ',' classificationIdent )*
  ;

#56.2 Refinable Classification   syntax diagram
refinableClassification :=
  '~' classificationIdent ( ',' '~' classificationIdent )*
  ;

#56.3 Classification Identifier   syntax diagram
classificationIdent := Ident ;

#57 Literal Compatibility   syntax diagram
literalCompatibility :=
  TLITERAL '=' protoLiteral ( '|' protoLiteral )*
  ;

#57.1 Proto-Literal   syntax diagram
protoLiteral :=
  protoLiteralIdent | structuredProtoLiteral
  ;

#57.2 Proto-Literal Identifier   syntax diagram
protoLiteralIdent := Ident ;

#58 Structured Proto-Literal   syntax diagram
structuredProtoLiteral :=
  '{'
    ( ARGLIST reqValueCount? OF
        ( '{' builtinOrReferential ( ',' builtinOrReferential )* '}' |
          builtinOrReferential ) ) |
    builtinOrReferential '}'
  ;

#58.1 Required Value Count   syntax diagram
reqValueCount :=
  greaterThan? wholeNumber
  ;
#58.2 Greater Than   syntax diagram
greaterThan := '>' ;

#58.3 Whole Number   syntax diagram
wholeNumber : NumberLiteral ;

#58.4 Built-in Type Or Referential Identifier   syntax diagram
builtinOrReferential := Ident ;

#59 Constraint   syntax diagram
constraint :=
  constraintTerm ( oneWayDependency | mutualDependencyOrExclusion )
  ;

#59.1 Constraint Term   syntax diagram
constraintTerm :=
  '(' classificationOrFlagIdent ')' |
  '[' bindableEntityOrProperty ']'
  ;

#59.2 Bindable Entity Or Property   syntax diagram
bindableEntityOrProperty :=
  entityToBindTo | propertyToBindTo
  ;

#59.3 One-Way Dependency   syntax diagram
oneWayDependency :=
  '->' termList ( '|' termList )*
  ;

#59.4 Mutual Dependency Or Exclusion   syntax diagram
mutualDependencyOrExclusion :=
  ( '<>' | '><' ) termList
  ;

#59.5 Term List   syntax diagram
termList :=
  constraintTerm ( ',' constraintTerm )*
  ;

#59.6 Classification Or Flag Identifier   syntax diagram
classificationOrFlagIdent := Ident ;

#60 Requirement   syntax diagram
requirement :=
  condition '->' ( typeRequirement | constRequirement | procRequirement )
  ;

#60.1 Condition   syntax diagram
condition :=
  NOT? boolConstIdent
  ;

#60.2 Boolean Constant Identifier   syntax diagram
boolConstIdent := Ident ;

#60.3 Type Requirement   syntax diagram
typeRequirement :=
  TYPE typeDefinition
  ;

#61 Constant Requirement   syntax diagram
constRequirement :=
  CONST
    ( '[' propertyToBindTo ']' ( simpleConstRequirement | '=' NONE ) |
      restrictedExport? simpleConstRequirement )
  ;

#61.1 Simple Constant Requirement   syntax diagram
simpleConstRequirement :=
  Ident ( '=' constExpression | ':' builtinTypeIdent )
  ;
#61.2 Constant Expression   syntax diagram
constExpression := expression ;

#61.3 Built-in Type Identifier   syntax diagram
builtinTypeIdent := Ident ;
#61.4 Restricted Export   syntax diagram
restrictedExport := '*' ;

#62 Property To Bind To   syntax diagram
propertyToBindTo :=
  memMgtProperty | collectionProperty | scalarProperty | TFLAGS
  ;

#62.1 Memory Management Property   syntax diagram
memMgtProperty :=
  TDYN | TREFC
  ;

#62.2 Collection Property   syntax diagram
collectionProperty :=
  TORDERED | TSORTED | TLIMIT
  ;

#62.3 Scalar Property   syntax diagram
scalarProperty :=
  TSCALAR | TMAX | TMIN
  ;

#63 Procedure Requirement   syntax diagram
procRequirement :=
  PROCEDURE
    ( '[' ( entityToBindTo | COROUTINE ) ']' ( procedureSignature | '=' NONE ) |
      restrictedExport? procedureSignature )
  ;

#64 Entity To Bind To   syntax diagram
entityToBindTo :=
  bindableResWord | bindableOperator | bindableMacro
  ;

#64.1 Bindable Reserved Word   syntax diagram
bindableResWord :=
  NEW | RETAIN | RELEASE | COPY | bindableFor
  ;

#64.2 Bindable FOR   syntax diagram
bindableFor :=
  FOR forBindingDifferentiator?
  ;

#64.3 FOR Binding Differentiator   syntax diagram
forBindingDifferentiator :=
  ':' ( '++' | '--' )
  ;

#64.4 Bindable Operator   syntax diagram
bindableOperator :=
  '+' | '-' | '*' | '/' | '\' | '=' | '<' | '>' | '::' |
  IN | DIV | MOD | unaryMinus
  ;

#64.5 Unary Minus   syntax diagram
unaryMinus := '+/-' ;

#64.6 Bindable Macro   syntax diagram
bindableMacro :=
  ABS | LENGTH | EXISTS | SUBSET | READ | READNEW | WRITE | WRITEF |
  SXF | VAL | multiBindableMacro1 | multiBindableMacro2 | multiBindableMacro3
  ;

#64.7 Multi-Bindable Macro 1   syntax diagram
multiBindableMacro1 :=
  ( COUNT | VALUE | SEEK ) bindingDifferentiator1?
  ;

#64.8 Binding Differentiator 1   syntax diagram
bindingDifferentiator1 :=
  ':' '#'
  ;

#64.9 Multi-Bindable Macro 2   syntax diagram
multiBindableMacro2 :=
  ( STORE | INSERT | REMOVE ) bindingDifferentiator2?
  ;

#64.10 Binding Differentiator 2   syntax diagram
bindingDifferentiator2 :=
  ':' ( ',' | '#' | '*' )
  ;

#64.11 Multi-Bindable Macro 3   syntax diagram
multiBindableMacro3 :=
  APPEND bindingDifferentiator3?
  ;

#64.12 Binding Differentiator 3   syntax diagram
bindingDifferentiator3 :=
  ':' ( ',' | '*' )
  ;