Common Syntax¶
Example of common syntax¶
1###########################################################
2# Example of Common Syntax suitable for testing Pygments.
3# The program does not make much sense, it’s just a test.
4# This comment block tests out end-of-line comments.
5###########################################################
6
7# This illustrates the keyword val, the := operator,
8# function calls, whitespace, symbols and punctuation.
9val name1 := cgiGet( 'name1' );
10val name2 := cgiGet( 'name2' );
11
12# This is a function definition which involves the
13# define/enddefine keywords and the => punctuation.
14define output() =>>
15 # Shows if/then/elseif/endif syntax and also
16 # strings and environment variable syntax.
17 if ${OUTPUT} = "html" then
18 # This illustrates the embedded XML syntax.
19 <html>
20 # Here we have text with interpolated expressions
21 # and an escaped HTML character entity.
22#### Issue: string interpolation.
23#### <title> "Welcome \(name1) \& \(name2)" </title>
24 <body>
25
26 </body>
27 </html>
28 elseif ${OUTPUT}... = "text"... then # Shows explodes.
29 # Loops, lists and vectors with numeric literals.
30 # Note use of parallel iteration operator.
31 {
32#### Issue: parallel iterators
33 for i in [ 2, 3, 5, 7, 11 ] #### // n from 1
34 do
35 tmp := newVector( i, i );
36 # Some arithmetic operators.
37 if 2 * n > i + 1 then
38 # The other postfix function appl. syntax.
39 # Also the indexing operator.
40 n @factorise -> i[ 1 ]
41 endif
42 endfor
43 } =: v; # Reverse short declaration operator.
44 else
45 # Shows postfix function application.
46 throw UnexpectedValue return _ with (
47 "$OUTPUT has unexpected value",
48 '$OUTPUT' -> ${OUTPUT}
49 )
50 endif
51enddefine;
52
53output().println; # And postfix function application.
Moar (test) examples¶
1# This will be built-in at some point soon.
2define print( x ) =>>
3 _print( x )
4enddefine;
5
6# So will this.
7define println( x ) =>>
8 _print( x );
9 _putchar( '\n' )
10enddefine;
11
12define hello() =>>
13 println( "hello, world!" )
14enddefine;
1define nfib( n ) =>>
2 if n <= 1 then
3 1
4 else
5 1 + nfib( n - 1 ) + nfib( n - 2 )
6 endif
7enddefine;
1define fact( n ) =>>
2 if n <= 1 then
3 1
4 else
5 n * fact( n - 1 )
6 endif
7enddefine;
Formal description¶
Use http://railroad.my28msec.com/rr/ui to produce a railroad diagram.
Package ::=
'package' PackageName ';' Import* TopLevelForms 'endpackage'
Import ::=
'import' ImportQualifier* Tags? from PackageName ( 'alias' AliasName )? ( 'into' Tags )? ';'
ImportQualifier ::=
'pervasive' |
'nonpervasive' |
'qualified' |
'unqualified'
Tags ::= '(' ( Tag ( ',' Tag )* )? ')'
TopLevelForms ::= ( Declaration | Statement ) ( ';' TopLevelForms )*
Declaration ::=
Definition |
Query |
RecordclassDeclaration
Definition ::=
'define' ApplyPattern '=>>' Statements 'enddefine'
RecordclassDeclaration ::=
'recordclass' Identifier
( 'slot' Identifier ';' )*
'endrecordclass'
Statements ::=
Statement ( ';' Statements )*
Statement ::=
Query |
Expr
Query ::=
Pattern (
':=' Expr |
'in' Expr |
FromByTo
)
Pattern ::=
Literal |
( ( 'var' | 'val' )? Tags )? Identifier |
Pattern ( (','|';') Pattern )+ |
'(' Pattern? ')' |
'[' Pattern? ']' |
'{' Pattern? '}' |
'[%' Pattern? '%]' |
'\(' Expression ')' |
ApplyPattern
ApplyPattern ::=
Identifier '(' Pattern? ')' |
Pattern ( '.'|'@') ( Identifier | '(' Expr ')' ) Pattern
FromByTo ::=
'from' Expr ('by' Expr )? ('to' Expr)? |
'by' Expr ('to' Expr)? |
'to' Expr .
Expr ::=
PrimaryExpr ( InfixOperator Expr )*
PrimaryExpr ::=
AtomicExpr |
AssignExpr |
ApplyExpr |
LambdaExpr |
ListExpr |
VectorExpr |
ConditionalExpr |
LoopExpr
AtomicExpr ::=
Literal |
Identifier |
'(' Statements? ')'
Literal ::=
'absent' |
( 'true' | 'false' ) |
Number |
CharacterConstant |
String
AssignExpr ::=
Expr '=::' TargetExpr |
TargetExpr '::=' Expr
TargetExpr ::=
Identifier |
ApplyExpr |
ConditionalTarget
ConditionalTarget ::=
'if' CoreConditionalTarget 'endif' |
'unless' CoreConditionalTarget 'endunless'
CoreConditionalTarget ::=
Expr then TargetExpr MoreConditionalTarget* 'else' TargetExpr .
MoreConditionalTarget ::=
( 'elseif' | 'elseunless' ) Expr then TargetExpr .
ApplyExpr ::=
AtomicExpr '(' Statements? ')' |
PrimaryExpr ( '.' | '@' ) AtomicExpr PrimaryExpr .
LambdaExpr ::=
'fn' AppExpr '=>>' Statements 'endfn'
ListExpr ::=
'[' Expr* ( '|' Expr )? ']'
VectorExpr ::=
'{' Expr '}'
ConditionalExpr ::=
'if' CoreConditional 'endif' |
'unless' CoreConditional 'endunless'
CoreConditional ::=
Expr then Statements MoreConditional* ( 'else' Statements )?
MoreConditional ::=
( 'elseif' | 'elseunless' ) Expr then Statements
LoopExpr ::=
'for' Query 'do' Statements 'endfor'
Number ::=
digit+ ( '.' digit+ )
String ::=
'"' QuotedCharacter* '"'
CharacterConstant ::=
"'" QuotedCharacter "'"
QuotedCharacter ::=
printing_character |
'\\' ( 'n' | 'r' | 's' | 't' | 'v' ) |
'\\&' HTMLEntity ';' |
'\\(' Expr ')'
InfixOperator ::= ',' | '+' | '-' | '*' | '/'