IF Commons GitHub

The printer and unit literals

0009Active

0009-sql-print · owner if · domain lang-sql

The printer

FR-001checkpassing

print(node, dialect, options?) MUST write any node of the tree as text of the dialect, quoting an identifier with the dialect's quotes only where identifier of 0007-sql-emit would, a string as the dialect's first string syntax writes it, and a parameter in the style the node has where the dialect reads it, else in the dialect's own; printScript(parsed, dialect, options?) MUST write a script with its statements ended by ;, an unparsed statement by its source text.

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: any node is written as text of the dialect: precedence is kept by parentheses where it asks for them, names and strings are quoted as the dialect does, and a script ends each statement with a semicolon; suite/print.test.ts: TABLESAMPLE after a table with no alias is read as what stands beside the table, not as its alias, and is written back

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts TABLESAMPLE after a table with no alias is read as what stands beside the table, not as it; passed: suite/print.test.ts any node is written as text of the dialect: precedence is kept by parentheses where it ask

Cited by: Lang Sql, print

FR-002checkpassing

What the printer writes MUST be read back by the parser of the same dialect as a tree that is the same as the printed one but for its ranges (toSexp without offsets), for every statement of the corpora that the parser reads into a tree whose kinds are all printable, and the statements for which it is not are held in a list that only shrinks.

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: what the printer writes is read back as the same tree for the corpora of every dialect, the statements that are not the same are counted, and the count only shrinks

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts what the printer writes is read back as the same tree for the corpora of every dialect, th

FR-003checkpassing

options.pretty MUST write each clause of a query on a line of its own, indented by two spaces for each level, and MUST read back as the same tree as without it.

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: the pretty form puts each clause on a line of its own, indents nested queries, and is read back as the same tree

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts the pretty form puts each clause on a line of its own, indents nested queries, and is read

Cited by: print

FR-004checkpassing

A tree MUST be printed in the dialect of another: the printer MUST NOT throw, and a construct that the other dialect does not read MUST be written as the printer's dialect has it (transpile in aide judges and reports the difference).

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: a tree is written for another dialect without throwing, as the printer's dialect has it

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts a tree is written for another dialect without throwing, as the printer's dialect has it

Cited by: Lang Sql, print

Unit literals

FR-005checkpassing

A grammar MUST be able to list units ({ unit, factor }, the unit a word of letters, upper case): a number followed by a word that is a unit (with or without white space) MUST be read as one number whose text is the product of the number and the factor, and a dialect that lists none MUST read the text as before.

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: a number followed by a unit is one number scaled by the unit, and a dialect without units reads the text as before

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts a number followed by a unit is one number scaled by the unit, and a dialect without units

Paging

FR-006checkpassing

A select or a set operation whose limitOptions is set MUST be written with its offset first and then FETCH FIRST n [PERCENT] ROWS ONLY | WITH TIES, in every dialect, and read back as the same tree; one without is written with LIMIT and OFFSET as before.

Enforced (check): .github: cd commons/lang-sql && bun run verify — suite/print.test.ts: FETCH FIRST with PERCENT or WITH TIES is read into the limit and its options, written with the offset first, read back as the same tree, and a FETCH FIRST n ROWS ONLY is still a LIMIT

Proved by: lang-sql/suite/print.test.ts

Tests: passed: suite/print.test.ts FETCH FIRST with PERCENT or WITH TIES is read into the limit and its options, written with

Out of scope

  • Custom top-level statements, which no consumer needs yet, and the comments of a tree (the printer writes none).

Edge cases

  • 10 KB with the unit KB of factor 1024: the number 10240; 1.5 KB: 1536; a unit that is not listed after a number: not read as a unit, per FR-005.

Assumptions

  • A unit's factor is a finite number and the product is written with no exponent for any value a person writes.

Open questions

  • None.

Key entities

  • Printer - the inverse of the parser.
  • Unit - a word that scales the number before it.

Success criteria

SC-001success criterion

A statement read in one dialect is written in it and read back as the same tree.

Review & acceptance checklist

  • [x] Every requirement is testable (MUST / MUST NOT), not aspirational
  • [x] No company fact is asserted here
  • [x] Every open item is marked, not silently decided

Cited by

Lang Sql, print

Titles, headings, exports, tables, columns and requirements. Esc closes.