Skip to content

Expression Parser Examples

The examples below assume that you have imported the components and set up the theme.

Basic

Evaluate a formula that the user types, against variables you provide. The static parse() turns the formula into an AST, and the static evaluate() runs that AST with a getter that returns each variable.

Formula price = 40, quantity = 3
Math.round(price * quantity * 0.9)
Result AST
<!-- Light theme for code editor -->
<link class="hljs-light" href="highlight.js/styles/atom-one-light.min.css" rel="stylesheet" />
<!-- Dark theme for code editor -->
<link class="hljs-dark" href="highlight.js/styles/atom-one-dark.min.css" rel="stylesheet" />
<div class="expression-calculator">
15 collapsed lines
<sv-code-editor class="formula-editor" aria-labelledby="formula-editor-name" one-line>
<span class="editor-header" slot="header">
<span id="formula-editor-name">Formula</span>
<code>price = 40, quantity = 3</code>
</span>
<pre>Math.round(price * quantity * 0.9)</pre>
</sv-code-editor>
<sv-code-editor class="result-editor" aria-labelledby="result-editor-name" one-line readonly>
<span id="result-editor-name" class="editor-header" slot="header">Result</span>
</sv-code-editor>
<sv-code-editor class="ast-editor" aria-labelledby="ast-editor-name" readonly>
<span id="ast-editor-name" class="editor-header" slot="header">AST</span>
</sv-code-editor>
</div>
<!-- Code editor setup: syntax highlighting and color theme, not related to the expression parser -->
<script type="application/x-typescript" module>
37 collapsed lines
import hljs from "highlight.js/lib/core";
import javascript from "highlight.js/lib/languages/javascript";
import json from "highlight.js/lib/languages/json";
const formulaEditor = document.querySelector<CodeEditor>(".formula-editor")!;
const resultEditor = document.querySelector<CodeEditor>(".result-editor")!;
const astEditor = document.querySelector<CodeEditor>(".ast-editor")!;
hljs.registerLanguage("javascript", javascript);
hljs.registerLanguage("json", json);
const highlightJson = (code: string) => hljs.highlight(code, { language: "json" }).value;
const escapeHtml = (code: string) => code.replaceAll("&", "&amp;").replaceAll("<", "&lt;");
formulaEditor.highlighter = code => hljs.highlight(code, { language: "javascript" }).value;
astEditor.highlighter = highlightJson;
resultEditor.highlighter = code =>
resultEditor.classList.contains("is-error") ? escapeHtml(code) : highlightJson(code);
// The docs site color scheme, a page of your own would pick one theme
const preferred = window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
const stored = document.documentElement.style.colorScheme;
const colorScheme = stored === "dark" || stored === "light" ? stored : preferred;
const editors = [formulaEditor, resultEditor, astEditor];
for (const editor of editors) {
editor.codeStylesheet = `.hljs-${colorScheme}`;
}
document.addEventListener("color-scheme-changed", event => {
const theme = (event as CustomEvent<"light" | "dark">).detail;
for (const editor of editors) {
editor.codeStylesheet = `.hljs-${theme}`;
}
});
</script>
<!-- Expression parser: parse the formula, evaluate it, and show the result and the AST -->
<script type="application/x-typescript" module>
31 collapsed lines
import type { ExpressionParser } from "@staticview/ui";
const Parser = customElements.get("sv-expression-parser") as typeof ExpressionParser;
const formulaEditor = document.querySelector<CodeEditor>(".formula-editor")!;
const resultEditor = document.querySelector<CodeEditor>(".result-editor")!;
const astEditor = document.querySelector<CodeEditor>(".ast-editor")!;
const variables = new Map([
["price", 40],
["quantity", 3],
]);
const evaluateFormula = () => {
astEditor.value = "";
try {
const ast = Parser.parse(formulaEditor.value);
astEditor.value = JSON.stringify(ast, null, 2);
const value = Parser.evaluate(ast, name => variables.get(name));
resultEditor.classList.remove("is-error");
resultEditor.value = JSON.stringify(value) ?? String(value);
} catch (error) {
resultEditor.classList.add("is-error");
resultEditor.value = (error as Error).message;
}
};
formulaEditor.addEventListener("valuechange", evaluateFormula);
evaluateFormula();
</script>
<style>
37 collapsed lines
.expression-calculator {
display: grid;
gap: 0.75em;
max-inline-size: 32rem;
margin-inline: auto;
}
.formula-editor {
--sv-code-editor-bg: var(--sv-gray-7);
}
.result-editor.is-error::part(highlight) {
color: var(--sv-error);
}
.ast-editor {
--sv-code-editor-font-sz: 1em;
&::part(textarea) {
block-size: 12lh;
}
}
.editor-header {
display: flex;
gap: 1em;
justify-content: space-between;
padding: 0.4em 0.8em;
font-size: 0.8em;
color: var(--sv-gray-3);
background-color: var(--sv-gray-6);
border-block-end: var(--sv-bdr-sz-sm) solid var(--sv-gray-5);
code {
color: var(--sv-gray-2);
}
}
</style>