compiler\parser.nim
implemented arrow like operators
This commit is contained in:
parent
7b480ea2ba
commit
2804c0373c
2 changed files with 36 additions and 37 deletions
|
|
@ -4,7 +4,7 @@ semicolon = ';' COMMENT?
|
||||||
colon = ':' COMMENT?
|
colon = ':' COMMENT?
|
||||||
colcom = ':' COMMENT?
|
colcom = ':' COMMENT?
|
||||||
|
|
||||||
operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
|
operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9 | OP10
|
||||||
| 'or' | 'xor' | 'and'
|
| 'or' | 'xor' | 'and'
|
||||||
| 'is' | 'isnot' | 'in' | 'notin' | 'of'
|
| 'is' | 'isnot' | 'in' | 'notin' | 'of'
|
||||||
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'addr' | 'static' | '..'
|
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'addr' | 'static' | '..'
|
||||||
|
|
@ -14,16 +14,17 @@ prefixOperator = operator
|
||||||
optInd = COMMENT?
|
optInd = COMMENT?
|
||||||
optPar = (IND{>} | IND{=})?
|
optPar = (IND{>} | IND{=})?
|
||||||
|
|
||||||
simpleExpr = assignExpr (OP0 optInd assignExpr)*
|
simpleExpr = arrowExpr (OP0 optInd arrowExpr)*
|
||||||
assignExpr = orExpr (OP1 optInd orExpr)*
|
arrowExpr = assignExpr (OP1 optInd assignExpr)*
|
||||||
orExpr = andExpr (OP2 optInd andExpr)*
|
assignExpr = orExpr (OP2 optInd orExpr)*
|
||||||
andExpr = cmpExpr (OP3 optInd cmpExpr)*
|
orExpr = andExpr (OP3 optInd andExpr)*
|
||||||
cmpExpr = sliceExpr (OP4 optInd sliceExpr)*
|
andExpr = cmpExpr (OP4 optInd cmpExpr)*
|
||||||
sliceExpr = ampExpr (OP5 optInd ampExpr)*
|
cmpExpr = sliceExpr (OP5 optInd sliceExpr)*
|
||||||
ampExpr = plusExpr (OP6 optInd plusExpr)*
|
sliceExpr = ampExpr (OP6 optInd ampExpr)*
|
||||||
plusExpr = mulExpr (OP7 optInd mulExpr)*
|
ampExpr = plusExpr (OP7 optInd plusExpr)*
|
||||||
mulExpr = dollarExpr (OP8 optInd dollarExpr)*
|
plusExpr = mulExpr (OP8 optInd mulExpr)*
|
||||||
dollarExpr = primary (OP9 optInd primary)*
|
mulExpr = dollarExpr (OP9 optInd dollarExpr)*
|
||||||
|
dollarExpr = primary (OP10 optInd primary)*
|
||||||
symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
|
symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
|
||||||
| IDENT
|
| IDENT
|
||||||
indexExpr = expr
|
indexExpr = expr
|
||||||
|
|
|
||||||
|
|
@ -5,24 +5,21 @@ This section lists Nim's standard syntax. How the parser handles
|
||||||
the indentation is already described in the `Lexical Analysis`_ section.
|
the indentation is already described in the `Lexical Analysis`_ section.
|
||||||
|
|
||||||
Nim allows user-definable operators.
|
Nim allows user-definable operators.
|
||||||
Binary operators have 10 different levels of precedence.
|
Binary operators have 11 different levels of precedence.
|
||||||
|
|
||||||
Relevant character
|
|
||||||
------------------
|
|
||||||
|
|
||||||
An operator symbol's *relevant character* is its first
|
|
||||||
character unless the first character is ``\`` and its length is greater than 1
|
|
||||||
then it is the second character.
|
|
||||||
|
|
||||||
This rule allows to escape operator symbols with ``\`` and keeps the operator's
|
|
||||||
precedence and associativity; this is useful for meta programming.
|
|
||||||
|
|
||||||
|
|
||||||
Associativity
|
Associativity
|
||||||
-------------
|
-------------
|
||||||
|
|
||||||
Binary operators whose relevant character is ``^`` are right-associative, all
|
Binary operators whose first character is ``^`` or its last character
|
||||||
other binary operators are left-associative.
|
is ``>`` are right-associative, all other binary operators are left-associative.
|
||||||
|
|
||||||
|
Exception: The single "greater than" ``>`` operator is left-associative too.
|
||||||
|
|
||||||
|
Operators ending in ``>`` but longer than a single character are
|
||||||
|
called `arrow like`:idx:.
|
||||||
|
|
||||||
|
|
||||||
Precedence
|
Precedence
|
||||||
----------
|
----------
|
||||||
|
|
@ -30,7 +27,7 @@ Precedence
|
||||||
Unary operators always bind stronger than any binary
|
Unary operators always bind stronger than any binary
|
||||||
operator: ``$a + b`` is ``($a) + b`` and not ``$(a + b)``.
|
operator: ``$a + b`` is ``($a) + b`` and not ``$(a + b)``.
|
||||||
|
|
||||||
If an unary operator's relevant character is ``@`` it is a `sigil-like`:idx:
|
If an unary operator's first character is ``@`` it is a `sigil-like`:idx:
|
||||||
operator which binds stronger than a ``primarySuffix``: ``@x.abc`` is parsed
|
operator which binds stronger than a ``primarySuffix``: ``@x.abc`` is parsed
|
||||||
as ``(@x).abc`` whereas ``$x.abc`` is parsed as ``$(x.abc)``.
|
as ``(@x).abc`` whereas ``$x.abc`` is parsed as ``$(x.abc)``.
|
||||||
|
|
||||||
|
|
@ -38,25 +35,26 @@ as ``(@x).abc`` whereas ``$x.abc`` is parsed as ``$(x.abc)``.
|
||||||
For binary operators that are not keywords the precedence is determined by the
|
For binary operators that are not keywords the precedence is determined by the
|
||||||
following rules:
|
following rules:
|
||||||
|
|
||||||
If the operator ends with ``=`` and its relevant character is none of
|
If the operator ends with ``=`` and its first character is none of
|
||||||
``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which
|
``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which
|
||||||
has the lowest precedence.
|
has the lowest precedence.
|
||||||
|
|
||||||
Otherwise precedence is determined by the relevant character.
|
Otherwise precedence is determined by the first character.
|
||||||
|
|
||||||
================ =============================================== ================== ===============
|
================ =============================================== ================== ===============
|
||||||
Precedence level Operators Relevant character Terminal symbol
|
Precedence level Operators First character Terminal symbol
|
||||||
================ =============================================== ================== ===============
|
================ =============================================== ================== ===============
|
||||||
9 (highest) ``$ ^`` OP9
|
10 (highest) ``$ ^`` OP10
|
||||||
8 ``* / div mod shl shr %`` ``* % \ /`` OP8
|
9 ``* / div mod shl shr %`` ``* % \ /`` OP9
|
||||||
7 ``+ -`` ``+ ~ |`` OP7
|
8 ``+ -`` ``+ ~ |`` OP8
|
||||||
6 ``&`` ``&`` OP6
|
7 ``&`` ``&`` OP7
|
||||||
5 ``..`` ``.`` OP5
|
6 ``..`` ``.`` OP6
|
||||||
4 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP4
|
5 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP5
|
||||||
3 ``and`` OP3
|
4 ``and`` OP4
|
||||||
2 ``or xor`` OP2
|
3 ``or xor`` OP3
|
||||||
1 ``@ : ?`` OP1
|
2 ``@ : ?`` OP2
|
||||||
0 (lowest) *assignment operator* (like ``+=``, ``*=``) OP0
|
1 *assignment operator* (like ``+=``, ``*=``) OP1
|
||||||
|
0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
|
||||||
================ =============================================== ================== ===============
|
================ =============================================== ================== ===============
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue