diff options
Diffstat (limited to 'compiler/syntaxes.nim')
-rw-r--r-- | compiler/syntaxes.nim | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/compiler/syntaxes.nim b/compiler/syntaxes.nim index 5fd81d87e..91cfdbefd 100644 --- a/compiler/syntaxes.nim +++ b/compiler/syntaxes.nim @@ -9,24 +9,24 @@ ## Implements the dispatcher for the different parsers. -import - strutils, llstream, ast, astalgo, idents, lexer, options, msgs, parser, +import + strutils, llstream, ast, astalgo, idents, lexer, options, msgs, parser, pbraces, filters, filter_tmpl, renderer -type - TFilterKind* = enum +type + TFilterKind* = enum filtNone, filtTemplate, filtReplace, filtStrip - TParserKind* = enum + TParserKind* = enum skinStandard, skinStrongSpaces, skinBraces, skinEndX -const +const parserNames*: array[TParserKind, string] = ["standard", "strongspaces", "braces", "endx"] filterNames*: array[TFilterKind, string] = ["none", "stdtmpl", "replace", "strip"] type - TParsers*{.final.} = object + TParsers*{.final.} = object skin*: TParserKind parser*: TParser @@ -42,53 +42,53 @@ proc parseTopLevelStmt*(p: var TParsers): PNode # implementation proc parseFile(fileIdx: int32): PNode = - var + var p: TParsers f: File let filename = fileIdx.toFullPathConsiderDirty if not open(f, filename): rawMessage(errCannotOpenFile, filename) - return + return openParsers(p, fileIdx, llStreamOpen(f)) result = parseAll(p) closeParsers(p) -proc parseAll(p: var TParsers): PNode = +proc parseAll(p: var TParsers): PNode = case p.skin of skinStandard, skinStrongSpaces: result = parser.parseAll(p.parser) - of skinBraces: + of skinBraces: result = pbraces.parseAll(p.parser) - of skinEndX: - internalError("parser to implement") + of skinEndX: + internalError("parser to implement") result = ast.emptyNode - -proc parseTopLevelStmt(p: var TParsers): PNode = + +proc parseTopLevelStmt(p: var TParsers): PNode = case p.skin of skinStandard, skinStrongSpaces: result = parser.parseTopLevelStmt(p.parser) - of skinBraces: + of skinBraces: result = pbraces.parseTopLevelStmt(p.parser) - of skinEndX: - internalError("parser to implement") + of skinEndX: + internalError("parser to implement") result = ast.emptyNode - -proc utf8Bom(s: string): int = - if (s[0] == '\xEF') and (s[1] == '\xBB') and (s[2] == '\xBF'): + +proc utf8Bom(s: string): int = + if (s[0] == '\xEF') and (s[1] == '\xBB') and (s[2] == '\xBF'): result = 3 - else: + else: result = 0 - -proc containsShebang(s: string, i: int): bool = - if (s[i] == '#') and (s[i + 1] == '!'): + +proc containsShebang(s: string, i: int): bool = + if (s[i] == '#') and (s[i + 1] == '!'): var j = i + 2 while s[j] in Whitespace: inc(j) result = s[j] == '/' -proc parsePipe(filename: string, inputStream: PLLStream): PNode = +proc parsePipe(filename: string, inputStream: PLLStream): PNode = result = ast.emptyNode var s = llStreamOpen(filename, fmRead) - if s != nil: + if s != nil: var line = newStringOfCap(80) discard llStreamReadLine(s, line) var i = utf8Bom(line) @@ -104,50 +104,50 @@ proc parsePipe(filename: string, inputStream: PLLStream): PNode = closeParser(q) llStreamClose(s) -proc getFilter(ident: PIdent): TFilterKind = - for i in countup(low(TFilterKind), high(TFilterKind)): - if identEq(ident, filterNames[i]): +proc getFilter(ident: PIdent): TFilterKind = + for i in countup(low(TFilterKind), high(TFilterKind)): + if identEq(ident, filterNames[i]): return i result = filtNone -proc getParser(ident: PIdent): TParserKind = - for i in countup(low(TParserKind), high(TParserKind)): - if identEq(ident, parserNames[i]): +proc getParser(ident: PIdent): TParserKind = + for i in countup(low(TParserKind), high(TParserKind)): + if identEq(ident, parserNames[i]): return i rawMessage(errInvalidDirectiveX, ident.s) -proc getCallee(n: PNode): PIdent = - if n.kind in nkCallKinds and n.sons[0].kind == nkIdent: +proc getCallee(n: PNode): PIdent = + if n.kind in nkCallKinds and n.sons[0].kind == nkIdent: result = n.sons[0].ident - elif n.kind == nkIdent: + elif n.kind == nkIdent: result = n.ident - else: + else: rawMessage(errXNotAllowedHere, renderTree(n)) - -proc applyFilter(p: var TParsers, n: PNode, filename: string, - stdin: PLLStream): PLLStream = + +proc applyFilter(p: var TParsers, n: PNode, filename: string, + stdin: PLLStream): PLLStream = var ident = getCallee(n) var f = getFilter(ident) case f - of filtNone: + of filtNone: p.skin = getParser(ident) result = stdin - of filtTemplate: + of filtTemplate: result = filterTmpl(stdin, filename, n) - of filtStrip: + of filtStrip: result = filterStrip(stdin, filename, n) - of filtReplace: + of filtReplace: result = filterReplace(stdin, filename, n) - if f != filtNone: + if f != filtNone: if hintCodeBegin in gNotes: rawMessage(hintCodeBegin, []) msgWriteln(result.s) rawMessage(hintCodeEnd, []) -proc evalPipe(p: var TParsers, n: PNode, filename: string, - start: PLLStream): PLLStream = +proc evalPipe(p: var TParsers, n: PNode, filename: string, + start: PLLStream): PLLStream = result = start - if n.kind == nkEmpty: return + if n.kind == nkEmpty: return if n.kind == nkInfix and n.sons[0].kind == nkIdent and identEq(n.sons[0].ident, "|"): for i in countup(1, 2): @@ -159,8 +159,8 @@ proc evalPipe(p: var TParsers, n: PNode, filename: string, result = evalPipe(p, n.sons[0], filename, result) else: result = applyFilter(p, n, filename, result) - -proc openParsers(p: var TParsers, fileIdx: int32, inputstream: PLLStream) = + +proc openParsers(p: var TParsers, fileIdx: int32, inputstream: PLLStream) = var s: PLLStream p.skin = skinStandard let filename = fileIdx.toFullPathConsiderDirty @@ -172,6 +172,6 @@ proc openParsers(p: var TParsers, fileIdx: int32, inputstream: PLLStream) = parser.openParser(p.parser, fileIdx, s, false) of skinStrongSpaces: parser.openParser(p.parser, fileIdx, s, true) - + proc closeParsers(p: var TParsers) = parser.closeParser(p.parser) |