Грамматика языка программирования Libretto

Дата и время генерации этого документа: 2025-01-17 21:04:19

Правила

Правила даны в синтаксисе грамматики, разбирающей выражение (см. википедию о PEG). ANY, MATCH, EOI - это предопределенные правила.

Parts ← FirstLineComment? Packages EOI
Packages ← ("package" AfterWord PackageName)? Globals?
Globals ← Sep? UseP* (Global (Sep Global)*)? Sep? OtherPackages*
OtherPackages ← "package" AfterWord PackageName Sep UseP* (Global (Sep Global)*)? Sep?
Global ← StructureP / Method / UnionP / TraitP / UnfoldMethod
UseP ← "use" AfterWord PackageName ("as" AfterWord LocalNameString)? Sep
KindContext ← Kind
Method ← "def" AfterWord (('~' LocalName) / (KindContext LocalName) / (MATCH LocalName)) Params? ColonType '=' Expression
KindNewContext ← KindNew
UnfoldMethod ← "unfold" AfterWord "def" AfterWord KindNewContext LocalName ParamsNew? ColonTypeNew '=' Expression
UnionP ← "union" AfterWord LocalName '=' (Kind (( '|' ) Kind)*)
TraitP ← "trait" AfterWord LocalName ('[' (LocalName ((',' ) LocalName)*) ']' ␣)? TraitBody?
TraitBody ← '{' (TraitMethod (Sep TraitMethod)*)? Sep? '}' ␣
TraitMethod ← ("def" AfterWord)? LocalName Params? ColonType
StructureP ← "struct" AfterWord LocalName ('[' (LocalName ((',' ) LocalName)*) ']' ␣)? (StructureBody1 / StructureBody2)?
StructureBody1 ← '(' (StructureField1 (( ',' ) StructureField1)*) ')' ␣
StructureBody2 ← '{' (StructureField2 (Sep StructureField2)*)? Sep? '}' ␣
StructureField1 ← LocalNameTypeBase
StructureField2 ← ("fix" AfterWord)? LocalNameTypeBase
Param ← ('#' (':' Name)? VarLocalName) / (VarLocalNameTypeBase ('=' Expression)?)
Params ← '(' (Param (( ',' ) Param)*)? ')' ␣
ParamsNew ← '(' (Param (( ',' ) Param)*) ')' ␣
ParamsN ← ('(' (Param (( ',' ) Param)*) ')' ␣) / MATCH
DefVarFixExpression ← ("fix" AfterWord VarLocalNameTypeBase '=' Expression) / ("var" AfterWord VarLocalNameTypeBase ('=' Expression)?) / ("def" AfterWord VarLocalName ParamsN ColonType '=' Expression)
Partial ← '{' (CaseP (Sep CaseP)*) '}' ␣
CaseP ← "case" AfterWord Pattern ("if" AfterWord Expression)? "=>" Expression
Pattern ← ExprBlock / PatternLiteral / TraitPattern / VarPattern
VarPattern ← ('_' ␣) / VarLocalName
TraitPattern ← VarLocalName ':' Name
SwitchContextExpr ← CaseList
Switch ← CaseList Block*
CaseList ← '{' (SwitchCase (Sep SwitchCase)*) (Sep CaseElse)? '}' ␣
CaseElse ← "else" AfterWord '='
SwitchOld ← '{' (SwitchCaseOld (Sep SwitchCaseOld)*) (Sep CaseElseOld)? '}' ␣ Block*
CaseElseOld ← "else" AfterWord "=>"
SwitchSimple ← SwitchCaseCaseOld ( SwitchCaseElseOld)?
SwitchCase ← SwitchCaseElse / SwitchCaseCase
SwitchCaseOld ← SwitchCaseElseOld / SwitchCaseCaseOld
SwitchCaseElse ← "else" AfterWord '=' Expression
SwitchCaseElseOld ← "else" AfterWord "=>"? Expression
SwitchCaseCase ← "case" AfterWord SwitchPattern ("if" AfterWord '(' Expression ')' )? '=' Expression
SwitchCaseCaseOld ← "case" AfterWord SwitchPatternOld ("if" AfterWord Expression)? "=>" Expression
SwitchPattern ← SwitchEmpty / SwitchNonEmpty / SwitchVar / SwitchExpr
SwitchPatternOld ← SwitchEmpty / SwitchNonEmptyOld / SwitchVarOld / SwitchExpr
SwitchVar ← (VarLocalName MandatoryColonType) / ('~' ␣ MandatoryColonType)
SwitchVarOld ← VarLocalNameOptTypeBase
SwitchExpr ← ExprBlock
SwitchEmpty ← "()" ␣
SwitchNonEmpty ← '(' (VarLocalName / ('~' ␣)) ')' ␣
SwitchNonEmptyOld ← '(' VarLocalName ')' ␣
Expression ← ReturnExpr / TopExpression
ReturnExpr ← "return" AfterWord TopExpression
TopExpression ← Infix
Infix ← Or (NameWithoutSpace RequiredSpaceNl Or)*
Or ← And ((("or" AfterWord) / ('∨' )) And)*
And ← Not ((("and" AfterWord) / ('∧' )) Not)*
Not ← ((("not" AfterWord) / ('¬' )) Not) / Compare
Compare ← Sum (("==" Sum) / ("!=" Sum) / (">=" Sum) / ('>' Sum) / ("<=" Sum) / ('<' Sum) / ("in" AfterWord Sum))*
Sum ← Mult (('+' Mult) / ('-' !'-' Mult))*
Mult ← CompoundExpr (('*' CompoundExpr) / ("mod" AfterWord CompoundExpr) / ("div" AfterWord CompoundExpr) / ("idiv" AfterWord CompoundExpr))*
CompoundExpr ← IfExpr / Path
IfExpr ← "if" AfterWord '(' Expression ')' (("as" AfterWord VarLocalName ExprBlock ( "else" AfterWord Expression)?) / (Expression ( "else" AfterWord Expression)?))
Path ← (DotPathStep ('.' PathStep)*) / (PathStep (('.' ) PathStep)*)
DotPathStep ← '.' NameOrCallNew AsBlock?
PathStep ← EmptyStep / OrderStep / GroupStep / IndexStep / BaseAndUpdatePathStep
EmptyStep ← '?' Seq
OrderStep ← "^(" (SubOrder ((',' ) SubOrder)*)? ')' ␣ Block*
SubOrder ← (("asc" AfterWord) / ("desc" AfterWord))? Expression
GroupStep ← '*' (Switch / SwitchOld / SwitchSimple / (NameOrCall Block*))
IndexValue ← (Negative? ".." Negative?) / Expression
IndexStep ← '[' IndexValue ']' ␣ Block*
BaseAndUpdatePathStep ← BasePathStep (('=' Expression) / ("+=" Expression) / (".=" Expression))?
BasePathStep ← StepExpr Block*
StepExpr ← IfExpr / ReturnExpr / (EnumExpr Apply*)
ColonArg ← ':' Expression
Apply ← '!' !'=' ␣ ApplyArg
ApplyArg ← ApplyArgExpr / ('(' (Expression (( ',' ) Expression)*)? ')' ␣)?
ApplyArgExpr ← StringTemplate / NewTrait / NameParametricOpt / Literal / Meta / ContextOrThis / Xml
EnumExpr ← Negative (".." Negative)?
Negative ← ('-' SimpleExpr) / SimpleExpr
SimpleExpr ← SwitchContextExpr / Partial / ExprBlock / ContextedExpr / Seq / Meta / Xml / ClassExpr / StringTemplate / NewTrait / NameOrCall / LiteralAll
NameOrCall ← NameExpr ('[' (SpecifiedType ((',' ) SpecifiedType)*) ']' ␣)? ((('(' (Expression (( ',' ) Expression)*)? ')' (␣ ColonArg)?) / ColonArg) ␣)?
NameOrCallNew ← NameExpr ('[' (SpecifiedTypeNew ((',' ) SpecifiedTypeNew)*) ']' ␣)? ((('(' (Expression (( ',' ) Expression)*)? ')' (␣ ColonArg)?) / ColonArg) ␣)?
NameParametricOpt ← NameExpr ('[' (SpecifiedType ((',' ) SpecifiedType)*) ']' ␣)?
ClassExpr ← '?' Name
Block ← ("as" AfterWord VarLocalName) / ("index" AfterWord LocalName)
AsBlock ← "as" AfterWord VarLocalName
ContextedExpr ← '$' (('{' ((DefVarFixExpression / Expression) (Sep (DefVarFixExpression / Expression))*)? Sep? '}' ␣) / (MATCH ␣))
ContextBlock ← "${" ((DefVarFixExpression / Expression) (Sep (DefVarFixExpression / Expression))*)? Sep? '}' ␣
ExprBlock ← ExprBlockNoTailSpace ␣
ExprBlockNoTailSpace ← '{' ((DefVarFixExpression / Expression) (Sep (DefVarFixExpression / Expression))*)? Sep? '}'
Seq ← SeqNoTailSpace ␣
SeqNoTailSpace ← '(' (Expression (( ',' ) Expression)*)? ')'
Literal ← IntegerOrRealOrExpExpr / String / CharOrSymbolExpr / UnitLiteral
LiteralAll ← IntegerOrRealOrExpExpr / String / CharOrSymbolExpr / ("unit" ␣) / ("this" ␣)
PatternLiteral ← ExpLiteralSigned / SignedReal / SignedInteger / String / Symbol / UnitLiteral
UnitLiteral ← "unit" ␣
NameExpr ← Name / ('!' ␣)
ContextOrThis ← ('$' / "this") ␣
Meta ← '#' (( (':' Name)? Params? ExprBlock) / GeneralType)
NewTrait ← "new" AfterWord WrapperSignature? '{' (WrapperMethod (Sep WrapperMethod)*)? Sep? '}' ␣
WrapperSignature ← (ParametricKind / Name) ('(' Expression ')' )?
WrapperMethod ← "def" AfterWord LocalName Params? ColonType '=' Expression
LocalNameTypeBase ← LocalName ColonType
VarLocalNameTypeBase ← VarLocalName ColonType
VarLocalNameOptTypeBase ← (VarLocalName / ('_' ␣)) ColonType
ColonType ← (':' (EmptyType / NothingType / GeneralType)) / CardOnlyTypeSemi / ␣
ColonTypeNew ← (':' (EmptyType / NothingType / GeneralType)) / ␣
MandatoryColonType ← ':' (EmptyType / NothingType / GeneralType)
SpecifiedType ← EmptyType / NothingType / OfType / OrType / KindType
SpecifiedTypeNew ← EmptyType / NothingType / OrType / KindTypeNew
GeneralType ← OfType / OrType / KindType / FreeType
EmptyType ← "()" ␣
NothingType ← '-' ␣
OfType ← ("typeof" / "type") '(' Expression ')' ␣
KindType ← Kind Card
KindTypeNew ← KindNew SpecifiedCardNew
OrType ← "general" '(' SpecifiedTypeNew ',' SpecifiedTypeNew ')' ␣
FreeType ← '~' Card
CardOnlyTypeSemi ← SignedCardSemi
Kind ← UnionKind / ParametricKind / Name
KindNew ← UnionKindNew / ParametricKindNew / Name
ParametricKind ← Name '[' (SpecifiedType ((',' ) SpecifiedType)*) ']' ␣
ParametricKindNew ← Name '[' (SpecifiedTypeNew ((',' ) SpecifiedTypeNew)*) ']' ␣
UnionKind ← '(' (Kind (('|' ) Kind)*) ')' ␣
UnionKindNew ← '(' (KindNew (('|' ) KindNew)*) ')' ␣
Card ← ('~' ␣) / SpecifiedCard
SpecifiedCard ← ('*' / '?' / '+' / '!') ␣
SpecifiedCardNew ← ('*' / '?' / '+') ␣
SignedCardSemi ← ('*' / '?' / '+' / '~' / '!') ␣
SignedCardStrict ← ('*' / '?' / '+' / '!') ␣
Xml ← (XmlElement / XmlComment) ␣
XmlElement ← '<' XmlPName XmlAttribute* (('>' XmlContents "</" XmlPName '>') / "/>")
XmlAttribute ← XmlPName '=' (('\"' (!('\"' / '\r' / '\n') AnyChar)* '\"' ) / ('\'' (!('\'' / '\r' / '\n') AnyChar)* '\'' ) / (XmlMetaAttr ))
XmlComment ← "<!--" XmlCommentText "-->"
XmlCommentText ← (!"-->" AnyChar)*
XmlText ← (!('<' / "#{") AnyChar)+
XmlMeta ← '#' XmlExpr
XmlMetaAttr ← '#' (XmlExpr / XmlSeq / XmlCall / XmlNamed)
XmlExpr ← ExprBlockNoTailSpace
XmlSeq ← SeqNoTailSpace
XmlNamed ← Name ';'
XmlCall ← Name '(' (Expression ((',' ) Expression)*)? ')'
XmlContents ← (XmlElement / XmlComment / XmlMeta / XmlText)*
XmlPName ← (XmlName ':')? XmlName
XmlFirstLetter7Bit ← [a-z] / [A-Z]
XmlLetter7Bit ← XmlFirstLetter7Bit / [0-9] / '-' / '.'
XmlFirstLetter ← XmlFirstLetter7Bit / [Ѐ-ӿ]
XmlLetter ← XmlLetter7Bit / [Ѐ-ӿ]
XmlName ← (XmlFirstLetter XmlLetter*) ␣
JsonTemplateBody ← JsonObject / JsonArray
JsonObject ← '{' ((JsonPair ) ((',' ) (JsonPair ))*)? '}'
JsonPair ← JsonString ':' JsonValue
JsonArray ← '[' ((JsonValue ) ((',' ) (JsonValue ))*)? ']'
JsonString ← String / Symbol
JsonInt ← SignedInteger
JsonReal ← SignedReal
JsonTrue ← "true"
JsonFalse ← "false"
JsonNull ← "null"
JsonExpLiteral ← ExpLiteralSigned
JsonExpr ← '#' ExprBlockNoTailSpace
JsonValue ← JsonObject / JsonArray / JsonExpr / JsonString / JsonTrue / JsonFalse / JsonNull / JsonExpLiteral / JsonReal / JsonInt
SignedInteger ← '-'? (("0x" (Hex+ ('_' Hex+)*)) / ("0b" (Bin+ ('_' Bin+)*)) / Digits) ␣
IntegerOrRealOrExpExpr ← (("0x" (Hex+ ('_' Hex+)*)) / ("0b" (Bin+ ('_' Bin+)*)) / (Digits (('.' Digits (EPart / MATCH)) / EPart / MATCH))) ␣
EPart ← ('e' / 'E') '-'? Digits
SignedReal ← '-'? Digits '.' Digits ␣
Digits ← Dec+ ('_' Dec+)*
ExpLiteralSigned ← '-'? Digits ('.' Digits)? EPart ␣
CharOrSymbolExpr ← '\'' (((('\\' Escaped) / (HighSurrogate LowSurrogate) / (!('\\' / '\'' / '\n' / '\r') AnyChar)) '\'' ␣) / (NameChars ␣))
Char ← '\'' (('\\' Escaped) / (HighSurrogate LowSurrogate) / (!('\\' / '\'' / '\n' / '\r') AnyChar)) '\'' ␣
Symbol ← '\'' NameChars ␣
HighSurrogate ← [?-?]
LowSurrogate ← [?-?]
Escaped ← '\\' / '\"' / '\'' / 'b' / 'n' / 'f' / 'r' / 't' / ('u' Hex Hex Hex Hex) / ("u{" Hex+ '}')
String ← (DString / NestedString) ␣
DString ← '\"' (('\\' Escaped) / (!('\\' / '\"' / '\n' / '\r') AnyChar))* '\"'
NestedString ← NestedStringA / NestedStringB / NestedStringC / NestedStringD
NestedStringOpen ← "<<" / '«' / '‘' / '“'
NestedStringA ← '«' ((!(NestedStringOpen / '»') AnyChar) / NestedString)* '»'
NestedStringB ← '‘' ((!(NestedStringOpen / '’') AnyChar) / NestedString)* '’'
NestedStringC ← '“' ((!(NestedStringOpen / '”') AnyChar) / NestedString)* '”'
NestedStringD ← "<<" ((!(NestedStringOpen / ">>") AnyChar) / NestedString)* ">>"
StringTextContent0 ← (!("#{" / ">>") AnyChar)+
StringTextContent1 ← (('\\' Escaped) / (!("#{" / ('\\' / '\"' / '\n' / '\r')) AnyChar))+
StringExpr ← '#' ExprBlockNoTailSpace
StringTemplate ← (("s<<" (StringExpr / StringTextContent0)* ">>") / ("s\"" (StringExpr / StringTextContent1)* '\"') / ("json\"" JsonTemplateBody '\"') / WrongTemplate) ␣
StringTemplateStrict ← (("s<<" (StringExpr / StringTextContent0)* ">>") / ("s\"" (StringExpr / StringTextContent1)* '\"') / ("json\"" JsonTemplateBody '\"')) ␣
WrongTemplate ← !("s<<" / "s\"" / "json\"") NameChars ('\"' / "<<")
Bin ← '0' / '1'
Dec ← [0-9]
Hex ← [0-9] / [a-f] / [A-F]
NameFirstLetter7Bit ← [a-z] / [A-Z] / '@'
NameLetter7Bit ← NameFirstLetter7Bit / [0-9] / '_'
NameFirstLetter ← NameFirstLetter7Bit / [Ѐ-ӿ]
NameLetter ← NameLetter7Bit / [Ѐ-ӿ]
NameChars ← NameFirstLetter NameLetter*
NameCharsValid ← NameChars
BacktickedPart ← '`' (!('`' / '/') AnyChar)+ '`'
NamePartWithoutSpace ← BacktickedPart / NameCharsValid
NamePart ← (BacktickedPart / NameCharsValid) ␣
NamePartStrictWithoutSpace ← BacktickedPart / NameChars
LocalNameString ← NamePartStrictWithoutSpace ␣
LocalName ← NamePartStrictWithoutSpace ␣
VarLocalName ← (BacktickedPart / ('~'? NameChars)) ␣
PackageName ← NamePart (('/' ) NamePart)*
Name ← NamePart ('/' NamePart)?
NameWithoutSpace ← NamePartWithoutSpace (␣ '/' NamePartWithoutSpace)?
AnyChar ← !('U+061C (ALM)' / 'U+200E (LRM)' / 'U+200F (RLM)' / 'U+202A (LRE)' / 'U+202B (RLE)' / 'U+202C (PDF)' / 'U+202D (LRO)' / 'U+202E (RLO)' / 'U+2066 (LRI)' / 'U+2067 (RLI)' / 'U+2068 (FSI)' / 'U+2069 (PDI)') ANY
FirstLineComment ← '#' (!Nl AnyChar)*
NestedComment ← "/*" (NestedComment / (!("/*" / "*/") AnyChar))* "*/"
Comment ← '/' (('/' (!Nl AnyChar)*) / ('*' (NestedComment / (!("/*" / "*/") AnyChar))* "*/"))
← (Comment / SpaceNlChar)*
␣ ← (Comment / SpaceChar)*
RequiredSpaceNl ← (Comment / SpaceNlChar)+
AfterWord ← (Comment / SpaceNlChar)+ / !NameFirstLetter
SpaceNlChar ← ' ' / '\n' / '\r' / '\t' / '\f'
SpaceChar ← ' ' / '\t' / '\f'
Cr ← '\r'
Nl ← '\n'
Sep ← (';' / (Cr* Nl))

Зарезервированные слова

Эти слова нельзя использовать в качестве идентификаторов:

2012-2025