big refactoring: step 1

This commit is contained in:
Araq 2016-10-31 15:34:44 +01:00
commit 773d17cd14
29 changed files with 278 additions and 313 deletions

View file

@ -30,30 +30,7 @@ type
skin*: TParserKind
parser*: TParser
proc parseFile*(fileIdx: int32): PNode{.procvar.}
proc openParsers*(p: var TParsers, fileIdx: int32, inputstream: PLLStream)
proc closeParsers*(p: var TParsers)
proc parseAll*(p: var TParsers): PNode
proc parseTopLevelStmt*(p: var TParsers): PNode
# implements an iterator. Returns the next top-level statement or nil if end
# of stream.
# implementation
proc parseFile(fileIdx: int32): PNode =
var
p: TParsers
f: File
let filename = fileIdx.toFullPathConsiderDirty
if not open(f, filename):
rawMessage(errCannotOpenFile, filename)
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)
@ -63,7 +40,7 @@ proc parseAll(p: var TParsers): PNode =
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)
@ -74,18 +51,18 @@ proc parseTopLevelStmt(p: var TParsers): PNode =
result = ast.emptyNode
proc utf8Bom(s: string): int =
if (s[0] == '\xEF') and (s[1] == '\xBB') and (s[2] == '\xBF'):
if s[0] == '\xEF' and s[1] == '\xBB' and s[2] == '\xBF':
result = 3
else:
result = 0
proc containsShebang(s: string, i: int): bool =
if (s[i] == '#') and (s[i + 1] == '!'):
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; cache: IdentCache): PNode =
result = ast.emptyNode
var s = llStreamOpen(filename, fmRead)
if s != nil:
@ -101,20 +78,20 @@ proc parsePipe(filename: string, inputStream: PLLStream): PNode =
inc(i, 2)
while line[i] in Whitespace: inc(i)
var q: TParser
parser.openParser(q, filename, llStreamOpen(substr(line, i)))
parser.openParser(q, filename, llStreamOpen(substr(line, i)), cache)
result = parser.parseAll(q)
parser.closeParser(q)
llStreamClose(s)
proc getFilter(ident: PIdent): TFilterKind =
for i in countup(low(TFilterKind), high(TFilterKind)):
if identEq(ident, filterNames[i]):
if cmpIgnoreStyle(ident.s, filterNames[i]) == 0:
return i
result = filtNone
proc getParser(ident: PIdent): TParserKind =
for i in countup(low(TParserKind), high(TParserKind)):
if identEq(ident, parserNames[i]):
if cmpIgnoreStyle(ident.s, parserNames[i]) == 0:
return i
rawMessage(errInvalidDirectiveX, ident.s)
@ -150,8 +127,7 @@ proc evalPipe(p: var TParsers, n: PNode, filename: string,
start: PLLStream): PLLStream =
result = start
if n.kind == nkEmpty: return
if n.kind == nkInfix and n.sons[0].kind == nkIdent and
identEq(n.sons[0].ident, "|"):
if n.kind == nkInfix and n[0].kind == nkIdent and n[0].ident.s == "|":
for i in countup(1, 2):
if n.sons[i].kind == nkInfix:
result = evalPipe(p, n.sons[i], filename, result)
@ -162,18 +138,31 @@ proc evalPipe(p: var TParsers, n: PNode, filename: string,
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;
cache: IdentCache) =
var s: PLLStream
p.skin = skinStandard
let filename = fileIdx.toFullPathConsiderDirty
var pipe = parsePipe(filename, inputstream)
var pipe = parsePipe(filename, inputstream, cache)
if pipe != nil: s = evalPipe(p, pipe, filename, inputstream)
else: s = inputstream
case p.skin
of skinStandard, skinBraces, skinEndX:
parser.openParser(p.parser, fileIdx, s, false)
parser.openParser(p.parser, fileIdx, s, cache, false)
of skinStrongSpaces:
parser.openParser(p.parser, fileIdx, s, true)
parser.openParser(p.parser, fileIdx, s, cache, true)
proc closeParsers(p: var TParsers) =
proc closeParsers*(p: var TParsers) =
parser.closeParser(p.parser)
proc parseFile*(fileIdx: int32; cache: IdentCache): PNode {.procvar.} =
var
p: TParsers
f: File
let filename = fileIdx.toFullPathConsiderDirty
if not open(f, filename):
rawMessage(errCannotOpenFile, filename)
return
openParsers(p, fileIdx, llStreamOpen(f), cache)
result = parseAll(p)
closeParsers(p)