closes #5070
This commit is contained in:
parent
b27e587087
commit
ccf99fa5ce
3 changed files with 14 additions and 23 deletions
|
|
@ -13,14 +13,10 @@ import
|
||||||
llstream, os, wordrecg, idents, strutils, ast, astalgo, msgs, options,
|
llstream, os, wordrecg, idents, strutils, ast, astalgo, msgs, options,
|
||||||
renderer, filters
|
renderer, filters
|
||||||
|
|
||||||
proc filterTmpl*(stdin: PLLStream, filename: string, call: PNode): PLLStream
|
|
||||||
# #! template(subsChar='$', metaChar='#') | standard(version="0.7.2")
|
|
||||||
# implementation
|
|
||||||
|
|
||||||
type
|
type
|
||||||
TParseState = enum
|
TParseState = enum
|
||||||
psDirective, psTempl
|
psDirective, psTempl
|
||||||
TTmplParser{.final.} = object
|
TTmplParser = object
|
||||||
inp: PLLStream
|
inp: PLLStream
|
||||||
state: TParseState
|
state: TParseState
|
||||||
info: TLineInfo
|
info: TLineInfo
|
||||||
|
|
@ -61,6 +57,10 @@ proc scanPar(p: var TTmplParser, d: int) =
|
||||||
proc withInExpr(p: TTmplParser): bool {.inline.} =
|
proc withInExpr(p: TTmplParser): bool {.inline.} =
|
||||||
result = p.par > 0 or p.bracket > 0 or p.curly > 0
|
result = p.par > 0 or p.bracket > 0 or p.curly > 0
|
||||||
|
|
||||||
|
const
|
||||||
|
LineContinuationOprs = {'+', '-', '*', '/', '\\', '<', '>', '^',
|
||||||
|
'|', '%', '&', '$', '@', '~', ','}
|
||||||
|
|
||||||
proc parseLine(p: var TTmplParser) =
|
proc parseLine(p: var TTmplParser) =
|
||||||
var j = 0
|
var j = 0
|
||||||
while p.x[j] == ' ': inc(j)
|
while p.x[j] == ' ': inc(j)
|
||||||
|
|
@ -77,7 +77,7 @@ proc parseLine(p: var TTmplParser) =
|
||||||
inc(j)
|
inc(j)
|
||||||
|
|
||||||
scanPar(p, j)
|
scanPar(p, j)
|
||||||
p.pendingExprLine = withInExpr(p) or llstream.endsWithOpr(p.x)
|
p.pendingExprLine = withInExpr(p) or p.x.endsWith(LineContinuationOprs)
|
||||||
case keyw
|
case keyw
|
||||||
of "end":
|
of "end":
|
||||||
if p.indent >= 2:
|
if p.indent >= 2:
|
||||||
|
|
@ -88,14 +88,14 @@ proc parseLine(p: var TTmplParser) =
|
||||||
llStreamWrite(p.outp, spaces(p.indent))
|
llStreamWrite(p.outp, spaces(p.indent))
|
||||||
llStreamWrite(p.outp, "#end")
|
llStreamWrite(p.outp, "#end")
|
||||||
of "if", "when", "try", "while", "for", "block", "case", "proc", "iterator",
|
of "if", "when", "try", "while", "for", "block", "case", "proc", "iterator",
|
||||||
"converter", "macro", "template", "method":
|
"converter", "macro", "template", "method", "func":
|
||||||
llStreamWrite(p.outp, spaces(p.indent))
|
llStreamWrite(p.outp, spaces(p.indent))
|
||||||
llStreamWrite(p.outp, substr(p.x, d))
|
llStreamWrite(p.outp, substr(p.x, d))
|
||||||
inc(p.indent, 2)
|
inc(p.indent, 2)
|
||||||
of "elif", "of", "else", "except", "finally":
|
of "elif", "of", "else", "except", "finally":
|
||||||
llStreamWrite(p.outp, spaces(p.indent - 2))
|
llStreamWrite(p.outp, spaces(p.indent - 2))
|
||||||
llStreamWrite(p.outp, substr(p.x, d))
|
llStreamWrite(p.outp, substr(p.x, d))
|
||||||
of "wLet", "wVar", "wConst", "wType":
|
of "let", "var", "const", "type":
|
||||||
llStreamWrite(p.outp, spaces(p.indent))
|
llStreamWrite(p.outp, spaces(p.indent))
|
||||||
llStreamWrite(p.outp, substr(p.x, d))
|
llStreamWrite(p.outp, substr(p.x, d))
|
||||||
if not p.x.contains({':', '='}):
|
if not p.x.contains({':', '='}):
|
||||||
|
|
@ -199,7 +199,7 @@ proc parseLine(p: var TTmplParser) =
|
||||||
inc(j)
|
inc(j)
|
||||||
llStreamWrite(p.outp, "\\n\"")
|
llStreamWrite(p.outp, "\\n\"")
|
||||||
|
|
||||||
proc filterTmpl(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
proc filterTmpl*(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
||||||
var p: TTmplParser
|
var p: TTmplParser
|
||||||
p.info = newLineInfo(filename, 0, 0)
|
p.info = newLineInfo(filename, 0, 0)
|
||||||
p.outp = llStreamOpen("")
|
p.outp = llStreamOpen("")
|
||||||
|
|
|
||||||
|
|
@ -13,14 +13,6 @@ import
|
||||||
llstream, os, wordrecg, idents, strutils, ast, astalgo, msgs, options,
|
llstream, os, wordrecg, idents, strutils, ast, astalgo, msgs, options,
|
||||||
renderer
|
renderer
|
||||||
|
|
||||||
proc filterReplace*(stdin: PLLStream, filename: string, call: PNode): PLLStream
|
|
||||||
proc filterStrip*(stdin: PLLStream, filename: string, call: PNode): PLLStream
|
|
||||||
# helpers to retrieve arguments:
|
|
||||||
proc charArg*(n: PNode, name: string, pos: int, default: char): char
|
|
||||||
proc strArg*(n: PNode, name: string, pos: int, default: string): string
|
|
||||||
proc boolArg*(n: PNode, name: string, pos: int, default: bool): bool
|
|
||||||
# implementation
|
|
||||||
|
|
||||||
proc invalidPragma(n: PNode) =
|
proc invalidPragma(n: PNode) =
|
||||||
localError(n.info, errXNotAllowedHere, renderTree(n, {renderNoComments}))
|
localError(n.info, errXNotAllowedHere, renderTree(n, {renderNoComments}))
|
||||||
|
|
||||||
|
|
@ -35,26 +27,26 @@ proc getArg(n: PNode, name: string, pos: int): PNode =
|
||||||
elif i == pos:
|
elif i == pos:
|
||||||
return n.sons[i]
|
return n.sons[i]
|
||||||
|
|
||||||
proc charArg(n: PNode, name: string, pos: int, default: char): char =
|
proc charArg*(n: PNode, name: string, pos: int, default: char): char =
|
||||||
var x = getArg(n, name, pos)
|
var x = getArg(n, name, pos)
|
||||||
if x == nil: result = default
|
if x == nil: result = default
|
||||||
elif x.kind == nkCharLit: result = chr(int(x.intVal))
|
elif x.kind == nkCharLit: result = chr(int(x.intVal))
|
||||||
else: invalidPragma(n)
|
else: invalidPragma(n)
|
||||||
|
|
||||||
proc strArg(n: PNode, name: string, pos: int, default: string): string =
|
proc strArg*(n: PNode, name: string, pos: int, default: string): string =
|
||||||
var x = getArg(n, name, pos)
|
var x = getArg(n, name, pos)
|
||||||
if x == nil: result = default
|
if x == nil: result = default
|
||||||
elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
|
elif x.kind in {nkStrLit..nkTripleStrLit}: result = x.strVal
|
||||||
else: invalidPragma(n)
|
else: invalidPragma(n)
|
||||||
|
|
||||||
proc boolArg(n: PNode, name: string, pos: int, default: bool): bool =
|
proc boolArg*(n: PNode, name: string, pos: int, default: bool): bool =
|
||||||
var x = getArg(n, name, pos)
|
var x = getArg(n, name, pos)
|
||||||
if x == nil: result = default
|
if x == nil: result = default
|
||||||
elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
|
elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "true") == 0: result = true
|
||||||
elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
|
elif x.kind == nkIdent and cmpIgnoreStyle(x.ident.s, "false") == 0: result = false
|
||||||
else: invalidPragma(n)
|
else: invalidPragma(n)
|
||||||
|
|
||||||
proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
proc filterStrip*(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
||||||
var pattern = strArg(call, "startswith", 1, "")
|
var pattern = strArg(call, "startswith", 1, "")
|
||||||
var leading = boolArg(call, "leading", 2, true)
|
var leading = boolArg(call, "leading", 2, true)
|
||||||
var trailing = boolArg(call, "trailing", 3, true)
|
var trailing = boolArg(call, "trailing", 3, true)
|
||||||
|
|
@ -68,7 +60,7 @@ proc filterStrip(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
||||||
llStreamWriteln(result, line)
|
llStreamWriteln(result, line)
|
||||||
llStreamClose(stdin)
|
llStreamClose(stdin)
|
||||||
|
|
||||||
proc filterReplace(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
proc filterReplace*(stdin: PLLStream, filename: string, call: PNode): PLLStream =
|
||||||
var sub = strArg(call, "sub", 1, "")
|
var sub = strArg(call, "sub", 1, "")
|
||||||
if len(sub) == 0: invalidPragma(call)
|
if len(sub) == 0: invalidPragma(call)
|
||||||
var by = strArg(call, "by", 2, "")
|
var by = strArg(call, "by", 2, "")
|
||||||
|
|
|
||||||
|
|
@ -84,7 +84,6 @@ const
|
||||||
AdditionalLineContinuationOprs = {'#', ':', '='}
|
AdditionalLineContinuationOprs = {'#', ':', '='}
|
||||||
|
|
||||||
proc endsWithOpr*(x: string): bool =
|
proc endsWithOpr*(x: string): bool =
|
||||||
# also used by the standard template filter:
|
|
||||||
result = x.endsWith(LineContinuationOprs)
|
result = x.endsWith(LineContinuationOprs)
|
||||||
|
|
||||||
proc continueLine(line: string, inTripleString: bool): bool {.inline.} =
|
proc continueLine(line: string, inTripleString: bool): bool {.inline.} =
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue