make from an operator (#14241)

This commit is contained in:
hlaaftana 2020-05-08 08:54:46 +03:00 • committed by GitHub
commit de27910ab7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 38 additions and 22 deletions

View file

@ -120,6 +120,8 @@
with writing typed macros. Old behavior for backwards compatiblity can be restored with writing typed macros. Old behavior for backwards compatiblity can be restored
with command line switch `--useVersion:1.0`. with command line switch `--useVersion:1.0`.
- The keyword `from` is now usable as an operator.
## Compiler changes ## Compiler changes
- Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`. - Specific warnings can now be turned into errors via `--warningAsError[X]:on|off`.

View file

@ -327,7 +327,7 @@ const
splitters = openPars + {tkComma, tkSemiColon} # do not add 'tkColon' here! splitters = openPars + {tkComma, tkSemiColon} # do not add 'tkColon' here!
oprSet = {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, 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 template goodCol(col): bool = col >= em.maxLineLen div 2

View file

@ -967,7 +967,7 @@ proc getPrecedence*(tok: TToken, strongSpaces: bool): int =
of '?': result = 2 of '?': result = 2
else: considerAsgn(2) else: considerAsgn(2)
of tkDiv, tkMod, tkShl, tkShr: result = 9 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 tkDotDot: result = 6
of tkAnd: result = 4 of tkAnd: result = 4
of tkOr, tkXor, tkPtr, tkRef: result = 3 of tkOr, tkXor, tkPtr, tkRef: result = 3

View file

@ -269,7 +269,8 @@ proc isRightAssociative(tok: TToken): bool {.inline.} =
proc isOperator(tok: TToken): bool = proc isOperator(tok: TToken): bool =
## Determines if the given token is an operator type token. ## Determines if the given token is an operator type token.
tok.tokType in {tkOpr, tkDiv, tkMod, tkShl, tkShr, tkIn, tkNotin, tkIs, 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 = proc isUnary(p: TParser): bool =
## Check if the current parser token is a unary operator ## 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 #| operator = OP0 | OP1 | OP2 | OP3 | OP4 | OP5 | OP6 | OP7 | OP8 | OP9
#| | 'or' | 'xor' | 'and' #| | 'or' | 'xor' | 'and'
#| | 'is' | 'isnot' | 'in' | 'notin' | 'of' #| | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from'
#| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..' #| | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
#| #|
#| prefixOperator = operator #| prefixOperator = operator
@ -555,7 +556,7 @@ proc parsePar(p: var TParser): PNode =
optInd(p, result) optInd(p, result)
flexComment(p, result) flexComment(p, result)
if p.tok.tokType in {tkDiscard, tkInclude, tkIf, tkWhile, tkCase, 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, tkConst, tkLet, tkWhen, tkVar, tkFor,
tkMixin}: tkMixin}:
# XXX 'bind' used to be an expression, so we exclude it here; # 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 #| | blockStmt | staticStmt | deferStmt | asmStmt
#| | 'proc' routine #| | 'proc' routine
#| | 'method' routine #| | 'method' routine
#| | 'func' routine
#| | 'iterator' routine #| | 'iterator' routine
#| | 'macro' routine #| | 'macro' routine
#| | 'template' routine #| | 'template' routine

View file

@ -6,7 +6,7 @@ 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
| 'or' | 'xor' | 'and' | 'or' | 'xor' | 'and'
| 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'is' | 'isnot' | 'in' | 'notin' | 'of' | 'as' | 'from' |
| 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..' | 'div' | 'mod' | 'shl' | 'shr' | 'not' | 'static' | '..'
prefixOperator = operator prefixOperator = operator
optInd = COMMENT? IND? optInd = COMMENT? IND?

View file

@ -565,7 +565,7 @@ following characters::
defined here.) defined here.)
These keywords are also operators: 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 `.`:tok: `=`:tok:, `:`:tok:, `::`:tok: are not available as general operators; they
are used for other notational purposes. are used for other notational purposes.
@ -638,21 +638,21 @@ has the second lowest precedence.
Otherwise precedence is determined by the first character. Otherwise precedence is determined by the first character.
================ ================================================== ================== =============== ================ ======================================================= ================== ===============
Precedence level Operators First character Terminal symbol Precedence level Operators First character Terminal symbol
================ ================================================== ================== =============== ================ ======================================================= ================== ===============
10 (highest) ``$ ^`` OP10 10 (highest) ``$ ^`` OP10
9 ``* / div mod shl shr %`` ``* % \ /`` OP9 9 ``* / div mod shl shr %`` ``* % \ /`` OP9
8 ``+ -`` ``+ - ~ |`` OP8 8 ``+ -`` ``+ - ~ |`` OP8
7 ``&`` ``&`` OP7 7 ``&`` ``&`` OP7
6 ``..`` ``.`` OP6 6 ``..`` ``.`` OP6
5 ``== <= < >= > != in notin is isnot not of as`` ``= < > !`` OP5 5 ``== <= < >= > != in notin is isnot not of as from`` ``= < > !`` OP5
4 ``and`` OP4 4 ``and`` OP4
3 ``or xor`` OP3 3 ``or xor`` OP3
2 ``@ : ?`` OP2 2 ``@ : ?`` OP2
1 *assignment operator* (like ``+=``, ``*=``) OP1 1 *assignment operator* (like ``+=``, ``*=``) OP1
0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0 0 (lowest) *arrow like operator* (like ``->``, ``=>``) OP0
================ ================================================== ================== =============== ================ ======================================================= ================== ===============
Whether an operator is used a prefix operator is also affected by preceding Whether an operator is used a prefix operator is also affected by preceding

View file

@ -0,0 +1,12 @@
discard """
nimout: '''
Infix
Ident "from"
Ident "a"
Ident "b"
'''
"""
from macros import dumpTree
dumpTree(a from b)