bowdy/engine/parser

Types

BroParserError = object of ValueError
  fatal*: bool
Parser = object
  inCallArgs*: bool
  outerIsColorCtor*: bool
  inValue*: bool
  inCaseBlock*: bool
  inBlockBody*: bool
  inForIterable*: bool
  arrayLits*: Table[string, Node]
PrefixFunction = proc (p: var Parser; minPrec = 0): Node

Consts

broValueFnNames = ["lighten", "darken", "saturate", "desaturate", "spin", "mix",
                   "mixCMYK", "toHex", "toHexAlpha", "toHtmlHex", "parseHex",
                   "parseHexAlpha", "parseHtmlHex", "parseColor",
                   "parseHtmlName", "parseHtmlColor", "distance", "almostEqual",
                   "parseLength", "parseAngle", "parseTime", "parseResolution",
                   "parseFlex", "var", "translate3d", "translate", "translateX",
                   "translateY", "translateZ", "scale", "scaleX", "scaleY",
                   "scaleZ", "scale3d", "rotate", "rotateX", "rotateY",
                   "rotateZ", "rotate3d", "skew", "skewX", "skewY", "matrix",
                   "matrix3d", "perspective", "blur", "brightness", "contrast",
                   "grayscale", "invert", "opacity", "saturate", "sepia",
                   "hue-rotate", "drop-shadow", "linear-gradient",
                   "repeating-linear-gradient", "radial-gradient",
                   "repeating-radial-gradient", "conic-gradient",
                   "repeating-conic-gradient", "circle", "ellipse", "inset",
                   "polygon", "path", "xywh", "ray", "cubic-bezier", "steps",
                   "linear", "rgb", "rgba", "hsl", "hsla", "hwb", "lab", "lch",
                   "oklab", "oklch", "color", "light-dark", "color-mix",
                   "image", "image-set"]
bowdy stdlib procs that construct or combine strictly typed CSS values (see stdlib/libcolors, stdlib/libcss). Calls to these names in property value position parse as real calls so they evaluate to typed values; every other name(...) stays opaque raw CSS text (rgb, calc, url, ...). Keep in sync with libcss.initCssTypes CSS function registrations.
colorCtorFns = ["linear-gradient", "repeating-linear-gradient",
                "radial-gradient", "repeating-radial-gradient",
                "conic-gradient", "repeating-conic-gradient", "drop-shadow",
                "var"]
Calls whose arguments accept pre-rendered static colors: gradient stops, drop-shadow parts and var() fallbacks render function spellings (rgba(0,0,0,.3)) verbatim. Compute functions (darken, mix, ...) are excluded so nested colors keep evaluating to typed values there.
cssFnBareKeywords = ["linear-gradient", "repeating-linear-gradient",
                     "radial-gradient", "repeating-radial-gradient",
                     "conic-gradient", "repeating-conic-gradient", "circle",
                     "ellipse", "inset", "polygon", "path", "xywh", "ray",
                     "steps", "rgb", "rgba", "hsl", "hsla", "hwb", "lab", "lch",
                     "oklab", "oklch", "color"]
CSS functions whose bare-identifier arguments are keywords, never variable reads (bowdy vars require $). Their args stringify at parse time so codegen never evaluates them as lookups (to right in gradients would otherwise die with undeclared identifier 'to').
cssFnNames = ["translate3d", "translate", "translateX", "translateY",
              "translateZ", "scale", "scaleX", "scaleY", "scaleZ", "scale3d",
              "rotate", "rotateX", "rotateY", "rotateZ", "rotate3d", "skew",
              "skewX", "skewY", "matrix", "matrix3d", "perspective", "blur",
              "brightness", "contrast", "grayscale", "invert", "opacity",
              "saturate", "sepia", "hue-rotate", "drop-shadow",
              "linear-gradient", "repeating-linear-gradient", "radial-gradient",
              "repeating-radial-gradient", "conic-gradient",
              "repeating-conic-gradient", "circle", "ellipse", "inset",
              "polygon", "path", "xywh", "ray", "cubic-bezier", "steps",
              "linear", "rgb", "rgba", "hsl", "hsla", "hwb", "lab", "lch",
              "oklab", "oklch", "color", "light-dark", "color-mix", "image",
              "image-set"]
CSS functions registered as real bowdy procs (see libcss.initCssTypes). Extend per wave; keep in sync with the registrations.
cssFnOpaqueNested = ["calc", "calc-size", "env", "attr", "url", "counter",
                     "counters", "symbols", "target-counter", "target-counters",
                     "target-text", "leader", "element", "anchor",
                     "anchor-size", "scroll", "view", "type", "min", "max",
                     "clamp", "round", "mod", "rem", "abs", "sign", "sin",
                     "cos", "tan", "asin", "acos", "atan", "atan2", "pow",
                     "sqrt", "hypot", "log", "exp", "minmax", "fit-content",
                     "cross-fade", "paint", "palette-mix"]
Functions that stay opaque permanently (expression containers like calc, UA/layout-dependent like env/anchor/counters). A real CSS function call nesting one of these falls back to verbatim text, so linear-gradient(url(x), red) keeps working without evaluating url.
cssFnSlashAlpha = ["rgb", "rgba", "hsl", "hsla", "hwb", "lab", "lch", "oklab",
                   "oklch", "color"]
Color functions with modern space syntax and slash alpha: rgb(255 0 0 / 50%). A trailing a / b infix is the alpha channel; it splices into separate args so bodies see components + alpha.
cssStaticRawColorFns = ["rgb", "rgba", "hsl", "hsla", "hwb", "lab", "lch",
                        "oklab", "oklch"]
Numeric color constructors whose fully-static calls keep their source spelling: separators (,, , `, space) and number forms (.3) don't survive parsing, and canonical re-rendering would rewrite real-world CSS (rgb(13 110 253), `rgba(0,0,0,.3)). Dynamic calls (any $var, var() or nested call) keep evaluating to typed colors canonically.
unitSizeSuffixes = ["px", "em", "rem", "%", "vh", "vw", "vmin", "vmax", "s",
                    "ms", "deg", "rad", "grad", "turn", "dpi", "dpcm", "dppx",
                    "Hz", "kHz", "fr", "ch", "ex", "cm", "mm", "in", "pt", "pc"]

Procs

proc errorContextFor(filePath: string; ln, col: int): string {....raises: [],
    tags: [ReadIOEffect], forbids: [].}
Source snippet for errors raised without lexer access (codegen). Re-reads the file; error path only. CodeGenError carries no filename, so context resolves against the entry file — errors inside imports may point at the wrong line.
proc isBareDefName(name: string): bool {.inline, ...raises: [], tags: [],
    forbids: [].}
Definition-site names (var/const, fn/mixin/iterator params, for-loop vars) must be bare identifiers starting with a letter. $name, _name (and empty names) are rejected; use sites require $.
proc isKnownCssProperty(name: string): bool {....raises: [KeyError, Exception],
    tags: [RootEffect], forbids: [].}
proc normVarName(name: string): string {....raises: [], tags: [], forbids: [].}
Normalize a declared variable name: var accent registers $accent. $ is required at use sites; already-prefixed (and --prefixed, e.g. custom properties) names pass through untouched.
proc normVarNode(node: Node): Node {....raises: [], tags: [], forbids: [].}
Apply normVarName to a declaration target (nkIdent, possibly wrapped in an export-marker nkPostfix). Anything else passes through.
proc parseScript(astProgram: var Ast; code: sink string; sourcePath: string) {.
    ...raises: [Exception, BroParserError, ValueError], tags: [RootEffect],
    forbids: [].}
Parse the given code into an AST.
proc parseScriptFile(astProgram: var Ast; path: string) {.
    ...raises: [OSError, Exception, BroParserError, ValueError, IOError],
    tags: [ReadIOEffect, RootEffect], forbids: [].}
Parse a .bass/.css file into an AST via a memory-mapped read. The mapping closes before returning; token values are copied out, so holding the AST afterwards is safe.