mirror of
https://github.com/nim-lang/Nim.git
synced 2026-01-06 04:57:49 +00:00
Merge pull request #2503 from ReneSac/arrowLike
Restrict arrow-like operators to those ending with `->`, `~>` or `=>`
This commit is contained in:
@@ -212,7 +212,8 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int =
|
||||
let relevantChar = tok.ident.s[0]
|
||||
|
||||
# arrow like?
|
||||
if L > 1 and tok.ident.s[L-1] == '>': return considerStrongSpaces(1)
|
||||
if L > 1 and tok.ident.s[L-1] == '>' and
|
||||
tok.ident.s[L-2] in {'-', '~', '='}: return considerStrongSpaces(1)
|
||||
|
||||
template considerAsgn(value: expr) =
|
||||
result = if tok.ident.s[L-1] == '=': 1 else: value
|
||||
|
||||
@@ -15,8 +15,6 @@ Associativity
|
||||
Binary operators whose first character is ``^`` are right-associative, all
|
||||
other binary operators are left-associative.
|
||||
|
||||
Operators ending in ``>`` but longer than a single character are
|
||||
called `arrow like`:idx:.
|
||||
|
||||
|
||||
Precedence
|
||||
@@ -33,9 +31,12 @@ 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:
|
||||
|
||||
Operators ending in either ``->``, ``~>`` or ``=>`` are called
|
||||
`arrow like`:idx:, and have the lowest precedence of all operators.
|
||||
|
||||
If the operator ends with ``=`` and its first character is none of
|
||||
``<``, ``>``, ``!``, ``=``, ``~``, ``?``, it is an *assignment operator* which
|
||||
has the lowest precedence.
|
||||
has the second lowest precedence.
|
||||
|
||||
Otherwise precedence is determined by the first character.
|
||||
|
||||
@@ -43,14 +44,14 @@ Otherwise precedence is determined by the first character.
|
||||
Precedence level Operators First character Terminal symbol
|
||||
================ =============================================== ================== ===============
|
||||
10 (highest) ``$ ^`` OP10
|
||||
9 ``* / div mod shl shr %`` ``* % \ /`` OP9
|
||||
8 ``+ -`` ``+ ~ |`` OP8
|
||||
9 ``* / div mod shl shr %`` ``* % \ /`` OP9
|
||||
8 ``+ -`` ``+ - ~ |`` OP8
|
||||
7 ``&`` ``&`` OP7
|
||||
6 ``..`` ``.`` OP6
|
||||
5 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP5
|
||||
5 ``== <= < >= > != in notin is isnot not of`` ``= < > !`` OP5
|
||||
4 ``and`` OP4
|
||||
3 ``or xor`` OP3
|
||||
2 ``@ : ?`` OP2
|
||||
2 ``@ : ?`` OP2
|
||||
1 *assignment operator* (like ``+=``, ``*=``) OP1
|
||||
0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
|
||||
================ =============================================== ================== ===============
|
||||
@@ -67,7 +68,7 @@ is still parsed as ``1 + (3 * 4)``, but ``1+3 * 4`` is parsed as ``(1+3) * 4``:
|
||||
|
||||
.. code-block:: nim
|
||||
#! strongSpaces
|
||||
if foo+4 * 4 == 8 and b&c | 9 ++
|
||||
if foo+4 * 4 == 8 and b&c | 9 ++
|
||||
bar:
|
||||
echo ""
|
||||
# is parsed as
|
||||
|
||||
@@ -15,6 +15,10 @@ true
|
||||
tester args
|
||||
all
|
||||
all args
|
||||
19
|
||||
-3
|
||||
false
|
||||
-2
|
||||
'''
|
||||
"""
|
||||
|
||||
@@ -67,3 +71,13 @@ const
|
||||
echo tester & " " & args|"all"
|
||||
echo "all" | tester & " " & args
|
||||
echo "all"|tester & " " & args
|
||||
|
||||
# Test arrow like operators. See also tests/macros/tclosuremacro.nim
|
||||
proc `+->`(a, b: int): int = a + b*4
|
||||
template `===>`(a, b: int): expr = a - b shr 1
|
||||
|
||||
echo 3 +-> 2 + 2 and 4
|
||||
var arrowed = 3+->2 + 2 and 4 # arrowed = 4
|
||||
echo arrowed ===> 15
|
||||
echo (2 * 3+->2) == (2*3 +-> 2)
|
||||
echo arrowed ===> 2 + 3+->2
|
||||
|
||||
@@ -34,6 +34,10 @@ News
|
||||
should be used instead.
|
||||
- ``nim idetools`` has been replaced by a separate tool `nimsuggest`_.
|
||||
- *arrow like* operators are not right associative anymore.
|
||||
- *arrow like* operators are now required to end with either ``->``, ``~>`` or
|
||||
``=>``, not just ``>``. Examples of operators still considered arrow like:
|
||||
``->``, ``==>``, ``+=>``. On the other hand, the following operators are now
|
||||
considered regular operators again: ``|>``, ``-+>``, etc.
|
||||
- Typeless parameters are now only allowed in templates and macros. The old
|
||||
way turned out to be too error-prone.
|
||||
- The 'addr' and 'type' operators are now parsed as unary function
|
||||
|
||||
Reference in New Issue
Block a user