From 3db3c0e3827a9a9bd70a8f635b0e6650c89b2b7b Mon Sep 17 00:00:00 2001 From: flaviut Date: Tue, 3 Jun 2014 10:12:31 -0400 Subject: [PATCH 1/8] Generalize accent parsing --- compiler/parser.nim | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index 2f9deb6b3b..61816f4433 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -147,8 +147,10 @@ proc expectIdent(p: TParser) = proc eat(p: var TParser, tokType: TTokType) = ## Move the parser to the next token if the current token is of type ## `tokType`, otherwise error. - if p.tok.tokType == tokType: getTok(p) - else: lexMessage(p.lex, errTokenExpected, TokTypeToStr[tokType]) + if p.tok.tokType == tokType: + getTok(p) + else: + lexMessage(p.lex, errTokenExpected, TokTypeToStr[tokType]) proc parLineInfo(p: TParser): TLineInfo = ## Retrieve the line information associated with the parser's current state. @@ -285,7 +287,7 @@ proc colcom(p: var TParser, n: PNode) = skipComment(p, n) proc parseSymbol(p: var TParser, allowNil = false): PNode = - #| symbol = '`' (KEYW|IDENT|operator|'(' ')'|'[' ']'|'{' '}'|'='|literal)+ '`' + #| symbol = '`' (KEYW|IDENT|operator|'('|')'|'['|']'|'{'|'}'|'='|literal)+ '`' #| | IDENT case p.tok.tokType of tkSymbol: @@ -296,29 +298,16 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode = getTok(p) while true: case p.tok.tokType - of tkBracketLe: - add(result, newIdentNodeP(getIdent"[]", p)) + of tkIntLit..tkCharLit, tkBracketLe, tkBracketRi, tkParLe, tkParRi, + tkCurlyRi, tkCurlyLe, tkEquals: + add(result, newIdentNodeP(getIdent(tokToStr(p.tok)), p)) getTok(p) - eat(p, tkBracketRi) - of tkEquals: - add(result, newIdentNodeP(getIdent"=", p)) - getTok(p) - of tkParLe: - add(result, newIdentNodeP(getIdent"()", p)) - getTok(p) - eat(p, tkParRi) - of tkCurlyLe: - add(result, newIdentNodeP(getIdent"{}", p)) - getTok(p) - eat(p, tkCurlyRi) of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDot, tkDotDot: add(result, newIdentNodeP(p.tok.ident, p)) getTok(p) - of tkIntLit..tkCharLit: - add(result, newIdentNodeP(getIdent(tokToStr(p.tok)), p)) - getTok(p) else: if result.len == 0: + echo repr p.tok parMessage(p, errIdentifierExpected, p.tok) break eat(p, tkAccent) From 7b1b3cbf2505f1b0f31349bd7f22c3c608820204 Mon Sep 17 00:00:00 2001 From: flaviut Date: Tue, 3 Jun 2014 10:12:57 -0400 Subject: [PATCH 2/8] More descriptive error message in semtypes enum --- compiler/semtypes.nim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index d88a956032..d70ed3465b 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -72,7 +72,7 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = e = n.sons[i].sym of nkIdent: e = newSymS(skEnumField, n.sons[i], c) - else: illFormedAst(n) + else: illFormedAst(n[i]) e.typ = result e.position = int(counter) if e.position == 0: hasNull = true From 20cb567bf5e404a30bb4b898b87865e4304b1130 Mon Sep 17 00:00:00 2001 From: flaviut Date: Tue, 3 Jun 2014 10:22:12 -0400 Subject: [PATCH 3/8] Fix accents in enums --- compiler/parser.nim | 8 +++++--- compiler/semtypes.nim | 5 +++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index 61816f4433..f89aee119b 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -296,20 +296,22 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode = of tkAccent: result = newNodeP(nkAccQuoted, p) getTok(p) + var bracketAccm = "" while true: case p.tok.tokType of tkIntLit..tkCharLit, tkBracketLe, tkBracketRi, tkParLe, tkParRi, tkCurlyRi, tkCurlyLe, tkEquals: - add(result, newIdentNodeP(getIdent(tokToStr(p.tok)), p)) + bracketAccm.add(tokToStr(p.tok)) getTok(p) of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDot, tkDotDot: add(result, newIdentNodeP(p.tok.ident, p)) getTok(p) else: - if result.len == 0: - echo repr p.tok + if result.len == 0 and bracketAccm == "": parMessage(p, errIdentifierExpected, p.tok) break + if bracketAccm != "": + result.add(newIdentNodeP(getIdent(bracketAccm), p)) eat(p, tkAccent) else: if allowNil and p.tok.tokType == tkNil: diff --git a/compiler/semtypes.nim b/compiler/semtypes.nim index d70ed3465b..2dcca8f1ef 100644 --- a/compiler/semtypes.nim +++ b/compiler/semtypes.nim @@ -70,9 +70,10 @@ proc semEnum(c: PContext, n: PNode, prev: PType): PType = counter = x of nkSym: e = n.sons[i].sym - of nkIdent: + of nkIdent, nkAccQuoted: e = newSymS(skEnumField, n.sons[i], c) - else: illFormedAst(n[i]) + else: + illFormedAst(n[i]) e.typ = result e.position = int(counter) if e.position == 0: hasNull = true From 7a9bcf47697a004de8d3b3ec54beb8ac4bdb9872 Mon Sep 17 00:00:00 2001 From: flaviut Date: Tue, 3 Jun 2014 18:58:59 -0400 Subject: [PATCH 4/8] Clean up code --- compiler/parser.nim | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index f89aee119b..ae115e749f 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -296,22 +296,17 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode = of tkAccent: result = newNodeP(nkAccQuoted, p) getTok(p) - var bracketAccm = "" + var accm = "" while true: case p.tok.tokType - of tkIntLit..tkCharLit, tkBracketLe, tkBracketRi, tkParLe, tkParRi, - tkCurlyRi, tkCurlyLe, tkEquals: - bracketAccm.add(tokToStr(p.tok)) - getTok(p) - of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDot, tkDotDot: - add(result, newIdentNodeP(p.tok.ident, p)) - getTok(p) - else: - if result.len == 0 and bracketAccm == "": + of tkAccent, tkEof: + if accm == "": parMessage(p, errIdentifierExpected, p.tok) break - if bracketAccm != "": - result.add(newIdentNodeP(getIdent(bracketAccm), p)) + else: + accm.add(tokToStr(p.tok)) + getTok(p) + result.add(newIdentNodeP(getIdent(accm), p)) eat(p, tkAccent) else: if allowNil and p.tok.tokType == tkNil: From 7e60cf2a3496eebe5757486230a5540405079980 Mon Sep 17 00:00:00 2001 From: flaviut Date: Tue, 3 Jun 2014 19:01:24 -0400 Subject: [PATCH 5/8] Add test --- tests/misc/tbug1217bracketquotes.nim | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 tests/misc/tbug1217bracketquotes.nim diff --git a/tests/misc/tbug1217bracketquotes.nim b/tests/misc/tbug1217bracketquotes.nim new file mode 100644 index 0000000000..90e67d45b6 --- /dev/null +++ b/tests/misc/tbug1217bracketquotes.nim @@ -0,0 +1,14 @@ +discard """ + output: "13{(.{}}{*4&*$**()&*@1235" +""" + +type + Test = enum + `1`, `3`, `{`, `(.`, `{}}{`, `*4&*$**()&*@` + +let `.}` = 1 +let `(}` = 2 +let `[` = 3 +let `]` = 5 + +echo `1`, `3`, `{`, `(.`, `{}}{`, `*4&*$**()&*@`, `.}`, `(}`, `[`, `]` From 90dc35c10998c279daf2b413aaf896cadaced699 Mon Sep 17 00:00:00 2001 From: flaviut Date: Wed, 4 Jun 2014 15:30:37 -0400 Subject: [PATCH 6/8] fix bug with unexpected tkInvalid,tkEof,tkComment --- compiler/parser.nim | 2 ++ 1 file changed, 2 insertions(+) diff --git a/compiler/parser.nim b/compiler/parser.nim index ae115e749f..ec829f6443 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -303,6 +303,8 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode = if accm == "": parMessage(p, errIdentifierExpected, p.tok) break + of tkEof, tkInvalid, tkComment: + parMessage(p, errIdentifierExpected, p.tok) else: accm.add(tokToStr(p.tok)) getTok(p) From 6b9359d90161887b2dc4fa12d9861883d22530f6 Mon Sep 17 00:00:00 2001 From: flaviut Date: Wed, 4 Jun 2014 15:40:31 -0400 Subject: [PATCH 7/8] updated grammer, fixed oversight --- compiler/parser.nim | 2 +- doc/grammar.txt | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/parser.nim b/compiler/parser.nim index ec829f6443..7b6ef8ee56 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -299,7 +299,7 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode = var accm = "" while true: case p.tok.tokType - of tkAccent, tkEof: + of tkAccent: if accm == "": parMessage(p, errIdentifierExpected, p.tok) break diff --git a/doc/grammar.txt b/doc/grammar.txt index 63e898e110..61557b68c4 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -24,7 +24,7 @@ ampExpr = plusExpr (OP6 optInd plusExpr)* plusExpr = mulExpr (OP7 optInd mulExpr)* mulExpr = dollarExpr (OP8 optInd dollarExpr)* dollarExpr = primary (OP9 optInd primary)* -symbol = '`' (KEYW|IDENT|operator|'(' ')'|'[' ']'|'{' '}'|'='|literal)+ '`' +symbol = '`' (KEYW|IDENT|operator|'('|')'|'['|']'|'{'|'}'|'='|literal)+ '`' | IDENT indexExpr = expr indexExprList = indexExpr ^+ comma @@ -166,7 +166,6 @@ object = 'object' pragma? ('of' typeDesc)? COMMENT? objectPart typeClassParam = ('var')? symbol typeClass = typeClassParam ^* ',' (pragma)? ('of' typeDesc ^* ',')? &IND{>} stmt -distinct = 'distinct' optInd typeDesc typeDef = identWithPragma genericParamList? '=' optInd typeDefAux indAndComment? varTuple = '(' optInd identWithPragma ^+ comma optPar ')' '=' optInd expr From 145cb3ae8ccf58495a3e38f6646041661de2affb Mon Sep 17 00:00:00 2001 From: flaviut Date: Sun, 8 Jun 2014 15:46:19 -0400 Subject: [PATCH 8/8] fix overlooked grammer comment --- compiler/parser.nim | 1 + doc/grammar.txt | 1 + 2 files changed, 2 insertions(+) diff --git a/compiler/parser.nim b/compiler/parser.nim index 7b6ef8ee56..0f52750c94 100644 --- a/compiler/parser.nim +++ b/compiler/parser.nim @@ -981,6 +981,7 @@ proc parseSymbolList(p: var TParser, result: PNode, allowNil = false) = proc parseTypeDescKAux(p: var TParser, kind: TNodeKind, mode: TPrimaryMode): PNode = + #| distinct = 'distinct' optInd typeDesc result = newNodeP(kind, p) getTok(p) optInd(p, result) diff --git a/doc/grammar.txt b/doc/grammar.txt index 61557b68c4..fe53418405 100644 --- a/doc/grammar.txt +++ b/doc/grammar.txt @@ -82,6 +82,7 @@ paramListColon = paramList? (':' optInd typeDesc)? doBlock = 'do' paramListArrow pragmas? colcom stmt doBlocks = doBlock ^* IND{=} procExpr = 'proc' paramListColon pragmas? ('=' COMMENT? stmt)? +distinct = 'distinct' optInd typeDesc expr = (ifExpr | whenExpr | caseExpr