diff options
author | hlaaftana <10591326+hlaaftana@users.noreply.github.com> | 2020-05-08 08:54:46 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-05-08 07:54:46 +0200 |
commit | de27910ab72dca681188953ae963802520271e43 (patch) | |
tree | 7ba0e839a7d659004ae9beb87d07bea31ee7e2fe | |
parent | 5fa7d374c4cb777372cf5b967575f228bda23c2b (diff) | |
download | Nim-de27910ab72dca681188953ae963802520271e43.tar.gz |
make `from` an operator (#14241)
-rw-r--r-- | changelog.md | 2 | ||||
-rw-r--r-- | compiler/layouter.nim | 2 | ||||
-rw-r--r-- | compiler/lexer.nim | 2 | ||||
-rw-r--r-- | compiler/parser.nim | 8 | ||||
-rw-r--r-- | doc/grammar.txt | 2 | ||||
-rw-r--r-- | doc/manual.rst | 32 | ||||
-rw-r--r-- | tests/parser/tstatementoperators.nim | 12 |
7 files changed, 38 insertions, 22 deletions
diff --git a/changelog.md b/changelog.md index 9ae50f1f3..32d7eba55 100644 --- a/changelog.md +++ b/changelog.md @@ -120,6 +120,8 @@ with writing typed macros. Old behavior for backwards compatiblity can be restored with command line switch `--useVersion:1.0`. +- The keyword `from` is now usable as an operator. + ## Compiler changes - Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`. diff --git a/compiler/layouter.nim b/compiler/layouter.nim index e137ee8a0..2477458f1 100644 --- a/compiler/layouter.nim +++ b/compiler/layouter.nim @@ -327,7 +327,7 @@ const splitters = openPars + {tkComma, tkSemiColon} # do not add 'tkColon' here! oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, - tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor} + tkIsnot, tkNot, tkOf, tkAs, tkFrom, tkDotDot, tkAnd, tkOr, tkXor} template goodCol(col): bool = col >= em.maxLineLen div 2 diff --git a/compiler/lexer.nim b/compiler/lexer.nim index e91a6415a..bd71a6ad5 100644 --- a/compiler/lexer.nim +++ b/compiler/lexer.nim @@ -967,7 +967,7 @@ proc getPrecedence*(tok: TToken, strongSpaces: bool): int = of '?': result = 2 else: considerAsgn(2) of tkDiv, tkMod, tkShl, tkShr: result = 9 - of tkIn, tkNotin, tkIs, tkIsnot, tkOf, tkAs: result = 5 + of tkIn, tkNotin, tkIs, tkIsnot, tkOf, tkAs, tkFrom: result = 5 of tkDotDot: result = 6 of tkAnd: result = 4 of tkOr, tkXor, tkPtr, tkRef: result = 3 diff --git a/compiler/parser.nim b/compiler/parser.nim index 4ea751057..9d1f6f97a 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -269,7 +269,8 @@ proc isRightAssociative(tok: TToken): bool {.inline.} = proc isOperator(tok: TToken): bool = ## Determines if the given token is an operator type token. tok.tokType in {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, - tkIsnot, tkNot, tkOf, tkAs, tkDotDot, tkAnd, tkOr, tkXor} + tkIsnot, tkNot, tkOf, tkAs, tkFrom, tkDotDot, tkAnd, + tkOr, tkXor} proc isUnary(p: TParser): bool = ## Check if the current parser token is a unary operator @@ -294,7 +295,7 @@ proc checkBinary(p: TParser) {.inline.} = #| #| operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9 #| | 'or' | 'xor' | 'and' -#| | 'is' | 'isnot' | 'in' | 'notin' | 'of' +#| | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from' #| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..' #| #| prefixOperator = operator @@ -555,7 +556,7 @@ proc parsePar(p: var TParser): PNode = optInd(p, result) flexComment(p, result) if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase, - tkTry, tkDefer, tkFinally, tkExcept, tkFor, tkBlock, + tkTry, tkDefer, tkFinally, tkExcept, tkBlock, tkConst, tkLet, tkWhen, tkVar, tkFor, tkMixin}: # XXX 'bind' used to be an expression, so we exclude it here; @@ -2193,6 +2194,7 @@ proc complexOrSimpleStmt(p: var TParser): PNode = #| | blockStmt | staticStmt | deferStmt | asmStmt #| | 'proc' routine #| | 'method' routine + #| | 'func' routine #| | 'iterator' routine #| | 'macro' routine #| | 'template' routine diff --git a/doc/grammar.txt b/doc/grammar.txt index 06eebbc1b..9d952d372 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -6,7 +6,7 @@ colon = ':' COMMENT? colcom = ':' COMMENT? operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9 | 'or' | 'xor' | 'and' - | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' + | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from' | | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..' prefixOperator = operator optInd = COMMENT? IND? diff --git a/doc/manual.rst b/doc/manual.rst index 85b307c80..02ccace2c 100644 --- a/doc/manual.rst +++ b/doc/manual.rst @@ -565,7 +565,7 @@ following characters:: defined here.) These keywords are also operators: -``and or not xor shl shr div mod in notin is isnot of as``. +``and or not xor shl shr div mod in notin is isnot of as from``. `.`:tok: `=`:tok:, `:`:tok:, `::`:tok: are not available as general operators; they are used for other notational purposes. @@ -638,21 +638,21 @@ has the second lowest precedence. 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 - 7 ``&`` ``&`` OP7 - 6 ``..`` ``.`` OP6 - 5 ``== <= < >= > != in notin is isnot not of as`` ``= < > !`` OP5 - 4 ``and`` OP4 - 3 ``or xor`` OP3 - 2 ``@ : ?`` OP2 - 1 *assignment operator* (like ``+=``, ``*=``) OP1 - 0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0 -================ ================================================== ================== =============== +================ ======================================================= ================== =============== +Precedence level Operators First character Terminal symbol +================ ======================================================= ================== =============== + 10 (highest) ``$ ^`` OP10 + 9 ``* / div mod shl shr %`` ``* % \ /`` OP9 + 8 ``+ -`` ``+ - ~ |`` OP8 + 7 ``&`` ``&`` OP7 + 6 ``..`` ``.`` OP6 + 5 ``== <= < >= > != in notin is isnot not of as from`` ``= < > !`` OP5 + 4 ``and`` OP4 + 3 ``or xor`` OP3 + 2 ``@ : ?`` OP2 + 1 *assignment operator* (like ``+=``, ``*=``) OP1 + 0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0 +================ ======================================================= ================== =============== Whether an operator is used a prefix operator is also affected by preceding diff --git a/tests/parser/tstatementoperators.nim b/tests/parser/tstatementoperators.nim new file mode 100644 index 000000000..d2b85bb4b --- /dev/null +++ b/tests/parser/tstatementoperators.nim @@ -0,0 +1,12 @@ +discard """ + nimout: ''' +Infix + Ident "from" + Ident "a" + Ident "b" +''' +""" + +from macros import dumpTree + +dumpTree(a from b) \ No newline at end of file |