liveBook · Manning

Number literal

As explained in Building User-Friendly DSLs

A DSL AST object representing a numeric literal value whose rendering can depend on the enclosing attribute's type.

A number literal is a DSL AST object holding a numeric value. Its projection renders the value directly, or adds a dollar prefix or percent postfix according to the enclosing Data Attribute’s type. section 4.2.3

Projecting a number literal requires these steps:

  • Pass the containing AST object as parent when projecting an initial value or attribute value. section 4.2.3
  • Check whether parent is a Data Attribute and read parent.settings["type"]. section 4.2.3
  • Render $ for amount, % for percentage, and no affix for other types. section 4.2.3

Context-sensitive projection

A Number with value 25 under an amount-typed Data Attribute renders as $25; under a percentage-typed attribute, it renders as 25%. section 4.2.3

case "Number": {
    const type = parent && parent.concept === "Data Attribute"
         && parent.settings["type"]
    return <div className="inline">
        {type === "amount" && <span className="keyword">$</span>}
        <span className="value">{settings["value"]}</span>
        {type === "percentage"
             && <span className="keyword">%</span>}
    </div>
}

The implementation combines parent-type detection with conditional JSX spans for the dollar prefix and percent postfix. section 4.2.3

How Building User-Friendly DSLs covers it

How deep it goes
Traces parent propagation and type checks into JSX rendering, without discussing parsing or numeric evaluation.
Examples
Shows amount values with $ prefixes, percentage values with % postfixes, and JSX spans for rendered numeric values.
What it assumes
Assumes AST containment and attribute settings, while demonstrating parent passing for upward context lookup.
What you can do after
Apply it via a React Projection component that renders numeric values according to their enclosing attribute type.
How it connects
Connects number literals to AST objects, Data Attributes, attribute types, parent traversal, and React projection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building User-Friendly DSLs. The section links point to the parts of the book it draws on.