AIScript Grammar
EBNF source grammar. Types and execution semantics constrain otherwise valid syntax.
program = { function } ;
function = name, "(", [ parameters ], ")", NEWLINE, block, "end", NEWLINE ;
parameters = name, { ",", name }, [ "," ] ;
block = { statement } ;
statement = assignment | expressionStatement | return | failure | throw
| if | for | while | switch | try | async | timer
| "break", NEWLINE | "continue", NEWLINE | "stop", NEWLINE ;
assignment = target, "=", expression, NEWLINE ;
target = name, { ".", name | "[", expression, "]" } ;
expressionStatement = expression, NEWLINE ;
return = "return", [ [ format ], expression, [ "with", object ] ], NEWLINE ;
format = "text" | "bytes" | "file" | "redirect" ;
failure = "fail", failureName, expression, NEWLINE ;
throw = "throw", name, NEWLINE ;
if = "if", expression, NEWLINE, block,
{ "else", "if", expression, NEWLINE, block },
[ "else", NEWLINE, block ], "end", NEWLINE ;
for = "for", "each", name, "in", expression, NEWLINE, block, "end", NEWLINE ;
while = "while", expression, NEWLINE, block, "end", NEWLINE ;
switch = "switch", expression, NEWLINE,
{ "case", expression, NEWLINE, block },
[ "else", NEWLINE, block ], "end", NEWLINE ;
try = "try", NEWLINE, block, { catch },
[ "finally", NEWLINE, block ], "end", NEWLINE ;
catch = "catch", ( failureName | name ), NEWLINE, block ;
async = "async", NEWLINE, block, "end", NEWLINE ;
timer = "timer", integerLiteral, elapsedUnit,
[ "timeout", integerLiteral, elapsedUnit ],
[ "repeat", integerLiteral ], NEWLINE, block, "end", NEWLINE ;
expression = orExpression ;
orExpression = andExpression, { "or", andExpression } ;
andExpression = comparison, { "and", comparison } ;
comparison = addition, [ comparisonOp, addition ] ;
comparisonOp = "==" | "!=" | "<" | "<=" | ">" | ">=" ;
addition = multiplication, { ( "+" | "-" ), multiplication } ;
multiplication = unary, { ( "*" | "/" | "%" ), unary } ;
unary = [ "not" | "-" ], postfix ;
postfix = primary, { ".", name | "[", expression, "]"
| "(", [ arguments ], ")" } ;
primary = name | stringLiteral | integerLiteral | decimalLiteral
| "true" | "false" | "null" | object | array
| "(", expression, ")" | service | processCall | sum ;
arguments = expression, { ",", expression }, [ "," ] ;
object = "{", [ entry, { ",", entry }, [ "," ] ], "}" ;
entry = ( name | stringLiteral ), ":", expression ;
array = "[", [ arguments ], "]" ;
service = "service", stringLiteral, [ "with", object ] ;
processCall = "call", stringLiteral, "with", expression ;
sum = "sum", expression, "for", "each", name, "in", expression ;
failureName = "InvalidInput" | "Unauthenticated" | "Forbidden" | "NotFound"
| "Conflict" | "LimitExceeded" | "Unavailable" | "InternalError" ;
elapsedUnit = "millisecond" | "milliseconds" | "second" | "seconds"
| "minute" | "minutes" | "hour" | "hours" ;
name = letter, { letter | digit | "_" } ;
letter = "A".."Z" | "a".."z" ;
digit = "0".."9" ;
integerLiteral = "0" | ( "1".."9", { digit } ) ;
decimalLiteral = integerLiteral, ( ".", digit, { digit }, [ exponent ] | exponent ) ;
exponent = ( "e" | "E" ), [ "+" | "-" ], digit, { digit } ;
stringLiteral = "\"", { stringCharacter | escape }, "\"" ;
stringCharacter = ? Unicode scalar other than quotation mark, reverse solidus or U+0000 through U+001F ? ;
escape = "\\", ( "\"" | "\\" | "/" | "b" | "f" | "n" | "r" | "t" | "u", hexDigit, hexDigit, hexDigit, hexDigit ) ;
hexDigit = digit | "a".."f" | "A".."F" ;
NEWLINE = ? one logical newline outside strings and bracketed expressions; blank lines and comments are ignored; EOF supplies a final newline ? ;