Skip to content

Commit

Permalink
comma separated props can share values
Browse files Browse the repository at this point in the history
Signed-off-by: George Lemon <[email protected]>
  • Loading branch information
georgelemon committed Aug 21, 2023
1 parent 570230c commit b6e8991
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 47 deletions.
1 change: 1 addition & 0 deletions src/bropkg/engine/ast.nim
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ type
pName*: string
pVal*: seq[Node] # a seq of CSS values
pRule*: PropertyRule
pShared*: seq[Node] # a seq of CSS properties sharing the same value
of ntFunction, ntMixin:
fnIdent*, fnName*: string
fnParams*: OrderedTable[string, ParamDef]
Expand Down
3 changes: 2 additions & 1 deletion src/bropkg/engine/compiler.nim
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
# Made by Humans from OpenPeeps
# https://github.com/openpeeps/bro

import pkg/[jsony, stashtable, chroma]
import pkg/[jsony, chroma]
import std/[tables, strutils, macros, sequtils, json,
math, oids, hashes, terminal, threadpool, enumutils]
import ./ast, ./sourcemap, ./stdlib, ./logging
Expand Down Expand Up @@ -187,6 +187,7 @@ proc sizeString(v: Node): string =

proc dumpHook*(s: var string, v: seq[Node])
proc dumpHook*(s: var string, v: CritBitTree[Node])
# proc dumpHook*(s: var string, v: Color)

proc dumpHook*(s: var string, v: Node) =
## Dumps `v` node to stringified JSON using `pkg/jsony`
Expand Down
25 changes: 22 additions & 3 deletions src/bropkg/engine/handlers/pSelector.nim
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,28 @@ newPrefixProc "parseProperty":
## Parse `key: value` pair as CSS Property
if likely(p.propsTable.hasKey(p.curr.value)):
let pName = p.curr
if p.next is tkColon:
walk p, 2
var pColon: TokenTuple
var shared: seq[Node]
if p.next in {tkColon, tkComma}:
walk p
pColon = p.next
result = newProperty(pName.value)
while p.curr.line == pName.line:
if unlikely(p.curr is tkComma):
while p.curr is tkComma:
walk p
if likely(p.propsTable.hasKey(p.curr.value)):
shared.add(newProperty(p.curr.value))
walk p
else: return nil
if likely(p.curr is tkColon):
pColon = p.curr
walk p
else: return nil
elif p.curr is tkColon:
pColon = p.curr
walk p
else: return nil
while p.curr.line == pColon.line:
# walk along the line and parse values
case p.curr.kind
of tkString:
Expand Down Expand Up @@ -157,6 +175,7 @@ newPrefixProc "parseProperty":
use(varCallNode)
else: break
if unlikely(p.curr is tkSemiColon): walk p
result.pShared = shared
return result
if p.next is tkColon:
let suggest = toSeq(p.propsTable.itemsWithPrefix(p.curr.value))
Expand Down
49 changes: 6 additions & 43 deletions src/bropkg/engine/parser.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{.warning[ImplicitDefaultValue]:off.}

import pkg/[stashtable, flatty, supersnappy, chroma,
import pkg/[flatty, supersnappy, chroma,
checksums/md5, malebolgia, malebolgia/ticketlocks]

import std/[os, strutils, sequtils, sequtils,
Expand Down Expand Up @@ -174,11 +174,6 @@ macro newPrefixProc(name: static string, body: untyped) =
nnkVarTy.newTree(ident("Parser")),
newEmptyNode()
),
# nnkIdentDefs.newTree(
# ident("scope"),
# nnkVarTy.newTree(nnkBracketExpr.newTree(ident("seq"), ident("ScopeTable"))),
# newEmptyNode()
# ),
nnkIdentDefs.newTree(
ident("excludeOnly"),
ident("includeOnly"),
Expand Down Expand Up @@ -516,6 +511,11 @@ proc parseSelectorStmt(p: var Parser, parent: (TokenTuple, Node), excludeOnly,
case node.nt:
of ntProperty:
parent[1].properties[node.pName] = node
if unlikely(node.pShared.len > 0):
for sharedProp in node.pShared:
sharedProp.pVal = node.pVal
parent[1].properties[sharedProp.pName] = sharedProp
node.pShared.setLen(0)
of ntCaseStmt, ntCondStmt, ntVariable,
ntAssign, ntCall, ntForStmt:
parent[1].innerNodes[$genOid()] = node
Expand Down Expand Up @@ -631,11 +631,7 @@ template startParseStylesheet(src, path: string) =
p.logger = Logger(filePath: "")
else:
p.logger = Logger(filePath: path)
# p.stylesheets = newStashTable[string, Stylesheet, 1000]()
# p.imports = newOrderedTable[string, Stylesheet]()
p.propsTable = initPropsTable()

# scope.add(ScopeTable())
p.program = Stylesheet()
p.program.sourcePath = path
p.program.setGlobalScope(ScopeTable())
Expand All @@ -648,43 +644,10 @@ template startParseStylesheet(src, path: string) =
if p.hasErrors: break
let node = p.parseRoot(excludeOnly = {tkExtend, tkPseudo, tkReturn})
if likely(node != nil):
# echo node
# echo ""
add p.program.nodes, node
# else: break
p.lex.close()

# proc importModule(th: ModuleThreadArgs) {.thread.} =
# {.gcsafe.}:
# proc newImportParser(): Parser =
# var p = Parser(filePath: th.path, dirPath: th.dirpath)
# # var scope = newSeq[ScopeTable]()
# if not th.isStdlib:
# startParseStylesheet(readFile(th.path), th.path)
# else:
# startParseStylesheet(th.code, th.path)
# result = p
# var subParser = newImportParser()
# if subParser.hasErrors:
# for error in subParser.logger.errors:
# display(error)
# display(" 👉 " & subParser.logger.filePath)
# return
# th.stylesheets.withValue(th.path):
# value[] = subParser.getStylesheet
# # if subParser.stylesheets.len > 0:
# discard th.stylesheets.addAll(subParser.stylesheets, false)


# proc moduleImporter(path, dirPath: string, results: ptr CountTable[Stylesheet], L: ptr TicketLock) {.gcsafe.} =
# var p = Parser(filePath: path, dirPath: dirPath)
# startParseStylesheet(readFile(path), path)
# if p.hasErrors:
# echo "error"
# else:
# withLock L[]:
# results[].inc p.getStylesheet

proc parseStylesheet*(src, path: string, enableCache = false): Parser =
var p = Parser()
when not defined wasm:
Expand Down

0 comments on commit b6e8991

Please sign in to comment.