mirror of
https://github.com/nim-lang/Nim.git
synced 2026-04-19 14:00:35 +00:00
compiler\parser.nim
implemented arrow like operators
This commit is contained in:
@@ -4,7 +4,7 @@ semicolon = ';' COMMENT?
|
||||
colon = ':' 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'
|
||||
| 'is' | 'isnot' | 'in' | 'notin' | 'of'
|
||||
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'addr' | 'static' | '..'
|
||||
@@ -14,16 +14,17 @@ prefixOperator = operator
|
||||
optInd = COMMENT?
|
||||
optPar = (IND{>} | IND{=})?
|
||||
|
||||
simpleExpr = assignExpr (OP0 optInd assignExpr)*
|
||||
assignExpr = orExpr (OP1 optInd orExpr)*
|
||||
orExpr = andExpr (OP2 optInd andExpr)*
|
||||
andExpr = cmpExpr (OP3 optInd cmpExpr)*
|
||||
cmpExpr = sliceExpr (OP4 optInd sliceExpr)*
|
||||
sliceExpr = ampExpr (OP5 optInd ampExpr)*
|
||||
ampExpr = plusExpr (OP6 optInd plusExpr)*
|
||||
plusExpr = mulExpr (OP7 optInd mulExpr)*
|
||||
mulExpr = dollarExpr (OP8 optInd dollarExpr)*
|
||||
dollarExpr = primary (OP9 optInd primary)*
|
||||
simpleExpr = arrowExpr (OP0 optInd arrowExpr)*
|
||||
arrowExpr = assignExpr (OP1 optInd assignExpr)*
|
||||
assignExpr = orExpr (OP2 optInd orExpr)*
|
||||
orExpr = andExpr (OP3 optInd andExpr)*
|
||||
andExpr = cmpExpr (OP4 optInd cmpExpr)*
|
||||
cmpExpr = sliceExpr (OP5 optInd sliceExpr)*
|
||||
sliceExpr = ampExpr (OP6 optInd ampExpr)*
|
||||
ampExpr = plusExpr (OP7 optInd plusExpr)*
|
||||
plusExpr = mulExpr (OP8 optInd mulExpr)*
|
||||
mulExpr = dollarExpr (OP9 optInd dollarExpr)*
|
||||
dollarExpr = primary (OP10 optInd primary)*
|
||||
symbol = '`' (KEYW|IDENT|literal|(operator|'('|')'|'['|']'|'{'|'}'|'=')+)+ '`'
|
||||
| IDENT
|
||||
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.
|
||||
|
||||
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
|
||||
-------------
|
||||
|
||||
Binary operators whose relevant character is ``^`` are right-associative, all
|
||||
other binary operators are left-associative.
|
||||
Binary operators whose first character is ``^`` or its last character
|
||||
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
|
||||
----------
|
||||
@@ -30,7 +27,7 @@ Precedence
|
||||
Unary operators always bind stronger than any binary
|
||||
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
|
||||
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
|
||||
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
|
||||
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
|
||||
8 ``* / div mod shl shr %`` ``* % \ /`` OP8
|
||||
7 ``+ -`` ``+ ~ |`` OP7
|
||||
6 ``&`` ``&`` OP6
|
||||
5 ``..`` ``.`` OP5
|
||||
4 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP4
|
||||
3 ``and`` OP3
|
||||
2 ``or xor`` OP2
|
||||
1 ``@ : ?`` OP1
|
||||
0 (lowest) *assignment operator* (like ``+=``, ``*=``) OP0
|
||||
10 (highest) ``$ ^`` OP10
|
||||
9 ``* / div mod shl shr %`` ``* % \ /`` OP9
|
||||
8 ``+ -`` ``+ ~ |`` OP8
|
||||
7 ``&`` ``&`` OP7
|
||||
6 ``..`` ``.`` OP6
|
||||
5 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP5
|
||||
4 ``and`` OP4
|
||||
3 ``or xor`` OP3
|
||||
2 ``@ : ?`` OP2
|
||||
1 *assignment operator* (like ``+=``, ``*=``) OP1
|
||||
0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
|
||||
================ =============================================== ================== ===============
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user