From f15b4ccb520b541b1bbaf2c0ae042b98bc8360cc Mon Sep 17 00:00:00 2001 From: ReneSac Date: Thu, 9 Apr 2015 17:00:11 -0300 Subject: [PATCH 1/7] Restrict arrow-like operators to start with - or = --- compiler/parser.nim | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index f135c540c0..f195892eb4 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -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[0] in {'-', '='}: + return considerStrongSpaces(1) template considerAsgn(value: expr) = result = if tok.ident.s[L-1] == '=': 1 else: value From de346400989ff6da0158d50f6f47cffac72f9593 Mon Sep 17 00:00:00 2001 From: ReneSac Date: Thu, 9 Apr 2015 17:01:40 -0300 Subject: [PATCH 2/7] Add tests for arrow like ops in tstrongspaces.nim --- tests/parser/tstrongspaces.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/parser/tstrongspaces.nim b/tests/parser/tstrongspaces.nim index 568abda4ca..c5b6115092 100644 --- a/tests/parser/tstrongspaces.nim +++ b/tests/parser/tstrongspaces.nim @@ -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 \ No newline at end of file From 5a524c7238d7a954071b954e1e7bb5fb381f265e Mon Sep 17 00:00:00 2001 From: ReneSac Date: Thu, 9 Apr 2015 17:17:47 -0300 Subject: [PATCH 3/7] Updated the news on *arrow like* breaking change --- web/news.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/web/news.txt b/web/news.txt index af44f91a1f..c515e2ec2c 100644 --- a/web/news.txt +++ b/web/news.txt @@ -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 start with ``=`` or ``-`` in + addition to end with ``>``. Examples of arrow like operators that continue + to work are: ``->``, ``==>``, ``=+>``. On the other hand, the following + operators are now considered regular operators: ``|>``, ``+->``, 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 From 8490699224023aa65cbfadbddf579199d7cb4221 Mon Sep 17 00:00:00 2001 From: ReneSac Date: Thu, 9 Apr 2015 17:31:56 -0300 Subject: [PATCH 4/7] Updated arrow like and strongSpaces in the manual Also added `-` to the first character table and standartized spaces there. --- doc/manual/syntax.txt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/manual/syntax.txt b/doc/manual/syntax.txt index cf44eb5881..0f2208d300 100644 --- a/doc/manual/syntax.txt +++ b/doc/manual/syntax.txt @@ -15,8 +15,8 @@ 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:. +Operators whose the first character is either ``-`` or ``=`` and the last +character is ``>`` are called `arrow like`:idx:. Precedence @@ -43,14 +43,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 +67,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 From d71f1b98e0c88acb30d109eb84a00d5ffc9e8cfe Mon Sep 17 00:00:00 2001 From: ReneSac Date: Thu, 9 Apr 2015 18:51:30 -0300 Subject: [PATCH 5/7] Restore newline at the end of the file. --- tests/parser/tstrongspaces.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/parser/tstrongspaces.nim b/tests/parser/tstrongspaces.nim index c5b6115092..eaf4d908c9 100644 --- a/tests/parser/tstrongspaces.nim +++ b/tests/parser/tstrongspaces.nim @@ -80,4 +80,4 @@ 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 \ No newline at end of file +echo arrowed ===> 2 + 3-+>2 From 34997292f48d40e8ed49d84c999b6d32afa3b044 Mon Sep 17 00:00:00 2001 From: ReneSac Date: Fri, 10 Apr 2015 23:59:17 -0300 Subject: [PATCH 6/7] Changing the rule for arrow like operators again. --- compiler/parser.nim | 2 +- doc/manual/syntax.txt | 4 ++-- tests/parser/tstrongspaces.nim | 10 +++++----- web/news.txt | 8 ++++---- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index f195892eb4..2d363638fc 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -212,7 +212,7 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int = let relevantChar = tok.ident.s[0] # arrow like? - if L > 1 and tok.ident.s[L-1] == '>' and tok.ident.s[0] in {'-', '='}: + if L > 1 and tok.ident.s[^1] == '>' and tok.ident.s[^2] in {'-', '~', '='}: return considerStrongSpaces(1) template considerAsgn(value: expr) = diff --git a/doc/manual/syntax.txt b/doc/manual/syntax.txt index 0f2208d300..f6fe0652db 100644 --- a/doc/manual/syntax.txt +++ b/doc/manual/syntax.txt @@ -15,8 +15,8 @@ Associativity Binary operators whose first character is ``^`` are right-associative, all other binary operators are left-associative. -Operators whose the first character is either ``-`` or ``=`` and the last -character is ``>`` are called `arrow like`:idx:. +Operators ending in either ``->``, ``~>`` or ``=>`` are called +`arrow like`:idx:. Precedence diff --git a/tests/parser/tstrongspaces.nim b/tests/parser/tstrongspaces.nim index eaf4d908c9..e70b91988c 100644 --- a/tests/parser/tstrongspaces.nim +++ b/tests/parser/tstrongspaces.nim @@ -73,11 +73,11 @@ 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 +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 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 +echo (2 * 3+->2) == (2*3 +-> 2) +echo arrowed ===> 2 + 3+->2 diff --git a/web/news.txt b/web/news.txt index c515e2ec2c..b5b897c1ab 100644 --- a/web/news.txt +++ b/web/news.txt @@ -34,10 +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 start with ``=`` or ``-`` in - addition to end with ``>``. Examples of arrow like operators that continue - to work are: ``->``, ``==>``, ``=+>``. On the other hand, the following - operators are now considered regular operators: ``|>``, ``+->``, etc. + - *arrow like* operators are now required 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 From 06617bbb76d4b9ecc0c40072ca472c4b3428514a Mon Sep 17 00:00:00 2001 From: ReneSac Date: Sat, 11 Apr 2015 17:22:09 -0300 Subject: [PATCH 7/7] Minor fixes for arrow like change patch Don't use ^ operator yet for compatibility with older compilers. Moved arrow like explanation, and fix precedence description on the text in the manual. Fixed typo in news. --- compiler/parser.nim | 4 ++-- doc/manual/syntax.txt | 7 ++++--- web/news.txt | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index 2d363638fc..5aeb834952 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -212,8 +212,8 @@ proc getPrecedence(tok: TToken, strongSpaces: bool): int = let relevantChar = tok.ident.s[0] # arrow like? - if L > 1 and tok.ident.s[^1] == '>' and tok.ident.s[^2] in {'-', '~', '='}: - 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 diff --git a/doc/manual/syntax.txt b/doc/manual/syntax.txt index f6fe0652db..24644bce2d 100644 --- a/doc/manual/syntax.txt +++ b/doc/manual/syntax.txt @@ -15,8 +15,6 @@ Associativity Binary operators whose first character is ``^`` are right-associative, all other binary operators are left-associative. -Operators ending in either ``->``, ``~>`` or ``=>`` 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. diff --git a/web/news.txt b/web/news.txt index b5b897c1ab..6b109aa1ca 100644 --- a/web/news.txt +++ b/web/news.txt @@ -34,7 +34,7 @@ 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 end with either ``->``, ``~>`` or + - *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.