Skip to content

Instantly share code, notes, and snippets.

@ozra
Last active August 29, 2015 14:22
Show Gist options
  • Save ozra/74b0c1afaea8c87040b3 to your computer and use it in GitHub Desktop.
Save ozra/74b0c1afaea8c87040b3 to your computer and use it in GitHub Desktop.
Pipes as alt. to `of` in Nim
proc primary(p: var TParser, mode: TPrimaryMode): PNode =
if isOperator(p.tok):
let isSigil = isSigilLike(p.tok)
result = newNodeP(nkPrefix, p)
var a = newIdentNodeP(p.tok.ident, p)
addSon(result, a)
getTok(p)
optInd(p, a)
if isSigil:
#XXX prefix operators
let baseInd = p.lex.currLineIndent
addSon(result, primary(p, pmSkipSuffix))
result = primarySuffix(p, result, baseInd)
else:
addSon(result, primary(p, pmNormal))
return
case p.tok.tokType:
| tkTuple: result = parseTuple(p, mode == pmTypeDef)
| tkProc: result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef})
| tkIterator:
when false:
if mode in {pmTypeDesc, pmTypeDef}:
result = parseProcExpr(p, false)
result.kind = nkIteratorTy
else:
# no anon iterators for now:
parMessage(p, errExprExpected, p.tok)
getTok(p) # we must consume a token here to prevend endless loops!
result = ast.emptyNode
else:
result = parseProcExpr(p, mode notin {pmTypeDesc, pmTypeDef})
if result.kind == nkLambda: result.kind = nkIteratorDef
else: result.kind = nkIteratorTy
| tkEnum:
if mode == pmTypeDef:
result = parseEnum(p)
else:
result = newNodeP(nkEnumTy, p)
getTok(p)
| tkObject:
if mode == pmTypeDef:
result = parseObject(p)
else:
result = newNodeP(nkObjectTy, p)
getTok(p)
| tkGeneric, tkConcept:
if mode == pmTypeDef:
let wasGeneric = p.tok.tokType == tkGeneric
result = parseTypeClass(p)
# hack so that it's remembered and can be marked as deprecated in
# sem'check:
if wasGeneric: result.flags.incl nfBase2
else:
parMessage(p, errInvalidToken, p.tok)
| tkStatic:
let info = parLineInfo(p)
getTokNoInd(p)
let next = primary(p, pmNormal)
if next.kind == nkBracket and next.sonsLen == 1:
result = newNode(nkStaticTy, info, @[next.sons[0]])
else:
result = newNode(nkStaticExpr, info, @[next])
| tkBind:
result = newNodeP(nkBind, p)
getTok(p)
optInd(p, result)
addSon(result, primary(p, pmNormal))
| tkVar: result = parseTypeDescKAux(p, nkVarTy, mode)
| tkRef: result = parseTypeDescKAux(p, nkRefTy, mode)
| tkPtr: result = parseTypeDescKAux(p, nkPtrTy, mode)
| tkDistinct: result = parseTypeDescKAux(p, nkDistinctTy, mode)
| _:
let baseInd = p.lex.currLineIndent
result = identOrLiteral(p, mode)
if mode != pmSkipSuffix:
result = primarySuffix(p, result, baseInd)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment