Skip to content

Commit

Permalink
parser handler parseAccQuoted rework
Browse files Browse the repository at this point in the history
  • Loading branch information
georgelemon committed Aug 4, 2023
1 parent 2e5887c commit 68955a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 55 deletions.
4 changes: 2 additions & 2 deletions src/bropkg/engine/compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pkg/jsony
import std/[tables, strutils, macros, sequtils, json,
algorithm, oids, terminal, enumutils]
algorithm, oids, hashes, terminal, enumutils]
import ./ast, ./sourcemap, ./logging

type
Expand Down Expand Up @@ -136,7 +136,7 @@ proc toString(c: var Compiler, v: Node, scope: ScopeTable = nil): string =
of ntAccQuoted:
var accValues: seq[string]
for accVar in v.accVars:
add accValues, accVar.callIdent[1..^1] # variable name without `$`
add accValues, "bro" & $(hash(accVar.callIdent[1..^1])) # variable name without `$`
add accValues, c.toString(c.getValue(accVar, scope))
v.accVal.format(accValues)
of ntStream:
Expand Down
32 changes: 23 additions & 9 deletions src/bropkg/engine/handlers/pLiteral.nim
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,26 @@ newPrefixProc "parseHSLAColor":

newPrefixProc "parseAccQuoted":
# parse string/var concat using backticks
result = newAccQuoted(p.curr.value)
if p.curr.attr.len != 0:
let acc = p.curr
for varName in acc.attr:
let varNode = p.parseVarCall(acc, "$" & varName, scope)
if unlikely(varNode == nil):
return nil
add result.accVars, varNode
else: walk p
let tk = p.curr
walk p
result = Node(nt: ntAccQuoted)
while p.curr isnot tkAccQuoted:
case p.curr.kind
of tkEOF: return nil
of tkVarSymbol:
let tkSymbol = p.curr
if p.next is tkLC:
walk p, 2
while p.curr isnot tkRC:
if unlikely(p.curr is tkEOF): return nil
add result.accVal, indent("$bro" & $(hash(p.curr.value)), tkSymbol.wsno)
if p.curr is tkIdentifier:
let varNode = p.parseVarCall(tk, "$" & p.curr.value, scope)
if likely(varNode != nil):
add result.accVars, varNode
else: return nil
walk p # tkRC
else:
add result.accVal, indent(p.curr.value, p.curr.wsno)
walk p
walk p # tkAccQuoted
22 changes: 6 additions & 16 deletions src/bropkg/engine/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,6 @@ proc parseVarCall(p: var Parser, tk: TokenTuple, varName: string, scope: var seq
#
# Parse utils
#
# when not defined release:
# proc `$`(node: Node): string = pretty(toJson(node), 2)
# proc `$`(program: Stylesheet): string = pretty(toJson(program), 2)
# else:
proc `$`(node: Node): string = $(toJson(node))
proc `$`(program: Stylesheet): string = $(toJson(program))

proc walk(p: var Parser, offset = 1) =
var i = 0
while offset > i:
Expand Down Expand Up @@ -239,8 +232,7 @@ proc stack(p: var Parser, node: Node, scope: var seq[ScopeTable]) =

proc getScope(p: var Parser, name: string, scopetables: var seq[ScopeTable]): tuple[st: ScopeTable, index: int] =
## Search through available `scopetables` and return
## the `ScopeTable` together with its `index` number.
## `index` is used by memoization module
## the `ScopeTable` followed by `index`
if scopetables.len > 0:
for i in countdown(scopetables.high, scopetables.low):
if scopetables[i].hasKey(name):
Expand Down Expand Up @@ -528,6 +520,7 @@ proc parseSelectorStmt(p: var Parser, parent: (TokenTuple, Node),
returnType = ntVoid, isFunctionWrap = false) =
if p.curr isnot tkEOF:
while p.curr.kind != tkEOF and (p.curr.line > parent[0].line and p.curr.pos > parent[0].pos):
let curr = p.curr
let node = p.parsePrefix(excludeOnly, includeOnly, scope, returnType, isFunctionWrap)
if likely(node != nil):
p.lastParent = parent[1]
Expand All @@ -540,11 +533,7 @@ proc parseSelectorStmt(p: var Parser, parent: (TokenTuple, Node),
parent[1].innerNodes[$node.condOid] = node
of ntCaseStmt:
parent[1].innerNodes[$node.caseOid] = node
of ntExtend:
discard
# if parent[1].properties.len != 0:
# error(extendMixedOrder, p.curr)
# else: discard
of ntExtend: discard
of ntCall:
parent[1].innerNodes[$node.callOid] = node
of ntCallStack:
Expand All @@ -553,13 +542,13 @@ proc parseSelectorStmt(p: var Parser, parent: (TokenTuple, Node),
# todo implement types for CSS selectors, example
parent[1].innerNodes[$node.stackOid] = node
else: error(invalidCallContext, p.prev)
else:
of ntClassSelector, ntIDSelector, ntPseudoSelector:
if likely(parent[1].innerNodes.hasKey(node.ident) == false):
parent[1].innerNodes[node.ident] = node
else:
for k, v in node.innerNodes:
parent[1].innerNodes[node.ident].innerNodes[k] = v
# parent[1].innerNodes[node.ident] = node
else: errorWithArgs(unexpectedToken, curr, [curr.value])
else: return
p.lastParent = nil

Expand Down Expand Up @@ -600,6 +589,7 @@ proc getPrefixFn(p: var Parser, excludeOnly, includeOnly: set[TokenKind] = {}):
of tkThis: parseThis
of tkAccQuoted: parseAccQuoted
of tkNamedColors: parseNamedColor
of tkAssert: parseAssert
else:
if p.next isnot tkColon:
parseSelectorTag
Expand Down
34 changes: 6 additions & 28 deletions src/bropkg/engine/tokens.nim
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ handlers:
add lex
else: break
if lex.token == "$":
lex.setError("Invalid variable")
lex.kind = tkVarSymbol
return
while lex.buf[lex.bufpos] == ' ':
inc lex.bufpos
Expand Down Expand Up @@ -111,36 +111,12 @@ handlers:
inc lex.bufpos # `
while true:
case lex.buf[lex.bufpos]
of '$':
add lex.token, '$'
inc lex.bufpos
if lex.current == '{':
inc lex.bufpos
var varName: string
while true:
case lex.buf[lex.bufpos]:
of '}':
inc lex.bufpos
break
of EndOfFile, NewLines:
lex.setError("EOF reached before closing curly bracket")
break
of IdentChars:
add varName, lex.buf[lex.bufpos]
inc lex.bufpos
else:
lex.setError("Invalid variable name")
break
if varName.len > 0:
add lex.token, varName
add lex.attr, varName
setLen(varName, 0)
of '`':
inc lex.bufpos
lex.kind = kind
break
of EndOfFile:
lex.setError("EOF reached before closing accent quoted string")
lex.setError("EOF reached before closing backtick string")
break
else:
add lex
Expand Down Expand Up @@ -243,15 +219,16 @@ registerTokens lexerSettings:
# excRule = tokenize(handleExclamation, '!')
hash = tokenize(handleHash, '#')
varConcat = tokenize(handleCurlyVar, '{')
varSymbol # $
varDef = tokenize(handleVariable, '$')
cssVarDef # $$default-size
varTyped
varCall
fnCall
# class = tokenize(handleClassSelector, '.')
class
dotExpr = '.'
accQuoted = tokenize(handleAccQuoted, '`')
# accQuoted = tokenize(handleAccQuoted, '`')
accQuoted = '`'
divide = '/':
comment = '/' .. EOL
at = '@':
Expand Down Expand Up @@ -281,6 +258,7 @@ registerTokens lexerSettings:
`in` = "in"
`when` = "when"
`echo` = "echo"
`assert` = "assert"
`return` = "return"
this = "this"
selectorType = "Selector"
Expand Down

0 comments on commit 68955a6

Please sign in to comment.