Generalize accent parsing

This commit is contained in:
flaviut 2014-06-03 10:12:31 -04:00
commit 3db3c0e382

View file

@ -147,8 +147,10 @@ proc expectIdent(p: TParser) =
proc eat(p: var TParser, tokType: TTokType) = proc eat(p: var TParser, tokType: TTokType) =
## Move the parser to the next token if the current token is of type ## Move the parser to the next token if the current token is of type
## `tokType`, otherwise error. ## `tokType`, otherwise error.
if p.tok.tokType == tokType: getTok(p) if p.tok.tokType == tokType:
else: lexMessage(p.lex, errTokenExpected, TokTypeToStr[tokType]) getTok(p)
else:
lexMessage(p.lex, errTokenExpected, TokTypeToStr[tokType])
proc parLineInfo(p: TParser): TLineInfo = proc parLineInfo(p: TParser): TLineInfo =
## Retrieve the line information associated with the parser's current state. ## 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) skipComment(p, n)
proc parseSymbol(p: var TParser, allowNil = false): PNode = proc parseSymbol(p: var TParser, allowNil = false): PNode =
#| symbol = '`' (KEYW|IDENT|operator|'(' ')'|'[' ']'|'{' '}'|'='|literal)+ '`' #| symbol = '`' (KEYW|IDENT|operator|'('|')'|'['|']'|'{'|'}'|'='|literal)+ '`'
#| | IDENT #| | IDENT
case p.tok.tokType case p.tok.tokType
of tkSymbol: of tkSymbol:
@ -296,29 +298,16 @@ proc parseSymbol(p: var TParser, allowNil = false): PNode =
getTok(p) getTok(p)
while true: while true:
case p.tok.tokType case p.tok.tokType
of tkBracketLe: of tkIntLit..tkCharLit, tkBracketLe, tkBracketRi, tkParLe, tkParRi,
add(result, newIdentNodeP(getIdent"[]", p)) tkCurlyRi, tkCurlyLe, tkEquals:
add(result, newIdentNodeP(getIdent(tokToStr(p.tok)), p))
getTok(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: of tokKeywordLow..tokKeywordHigh, tkSymbol, tkOpr, tkDot, tkDotDot:
add(result, newIdentNodeP(p.tok.ident, p)) add(result, newIdentNodeP(p.tok.ident, p))
getTok(p) getTok(p)
of tkIntLit..tkCharLit:
add(result, newIdentNodeP(getIdent(tokToStr(p.tok)), p))
getTok(p)
else: else:
if result.len == 0: if result.len == 0:
echo repr p.tok
parMessage(p, errIdentifierExpected, p.tok) parMessage(p, errIdentifierExpected, p.tok)
break break
eat(p, tkAccent) eat(p, tkAccent)