Skip to content

Commit

Permalink
Parse StringConstructors
Browse files Browse the repository at this point in the history
No need to process them yet. Parsing them is enough: they are used in source files that I'm trying
to process.

Look like this:

```xq
let $str := ``[My string!
With newlines!
And `{fn:date()}` interpolation~
]`` return $str
```
  • Loading branch information
DrRataplan committed Oct 26, 2024
1 parent 0618992 commit 40e08de
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 57 deletions.
54 changes: 53 additions & 1 deletion src/parsing/prscParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ import {
import * as tokens from './tokens';
import { atomicOrUnionType, singleType, typeName } from './typesParser';
import {
char,
explicitWhitespace,
whitespace,
whitespaceCache,
Expand Down Expand Up @@ -713,6 +714,57 @@ function generateParser(options: { outputDebugInfo: boolean; xquery: boolean }):
(x) => ['arrayConstructor', x],
);

const stringConstructorChars: Parser<IAST> = map(
star(
preceded(
peek(
not(
or([
tokens.STRING_INTERPOLATION_OPEN,
tokens.STRING_INTERPOLATION_CLOSE,
tokens.STRING_CONSTRUCTOR_CLOSE,
]),
['String constructors can not contain interpolation characters'],
),
),
char,
),
),
(x) => ['stringConstructorChars', x.join('')],
);

const stringConstructorInterpolation: Parser<IAST> = map(
delimited(tokens.STRING_INTERPOLATION_OPEN, expr, tokens.STRING_INTERPOLATION_CLOSE, true),
(ast) => ['stringConstructorInterpolation', ast],
);

const stringConstructorContent: Parser<IAST[]> = then(
stringConstructorChars,
star(
then(stringConstructorInterpolation, stringConstructorChars, (interpolation, chars) => [
interpolation,
chars,
]),
),
(a, b) => {
const toReturn = [a];
for (const [interpolation, chars] of b) {
toReturn.push(interpolation, chars);
}
return toReturn;
},
);

const stringConstructor: Parser<IAST> = map(
delimited(
tokens.STRING_CONSTRUCTOR_OPEN,
stringConstructorContent,
tokens.STRING_CONSTRUCTOR_CLOSE,
true,
),
(contents) => ['stringConstructor', ...contents],
);

const keySpecifier: Parser<string | IAST> = or([
ncName as Parser<string | IAST>,
integerLiteral,
Expand Down Expand Up @@ -986,7 +1038,7 @@ function generateParser(options: { outputDebugInfo: boolean; xquery: boolean }):
functionItemExpr,
mapConstructor,
arrayConstructor,
// stringConstructor,
stringConstructor,
unaryLookup,
]);

Expand Down
6 changes: 6 additions & 0 deletions src/parsing/tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,9 @@ export const NAMESPACE_NODE_TEST = token('namespace-node()');
export const ANY_KIND_TEST = token('node()');
export const ITEM_TYPE_TEST = token('item()');
export const EMPTY_SEQUENCE_TEST = token('empty-sequence()');

export const BACKTICK = token('`');
export const STRING_CONSTRUCTOR_OPEN = token('``[');
export const STRING_CONSTRUCTOR_CLOSE = token(']``');
export const STRING_INTERPOLATION_OPEN = token('`{');
export const STRING_INTERPOLATION_CLOSE = token('}`');
25 changes: 0 additions & 25 deletions test/assets/failingXQueryXTestNames.csv
Original file line number Diff line number Diff line change
Expand Up @@ -2641,35 +2641,10 @@ K2-Steps-19,result was not equal
K2-Steps-20,result was not equal
K2-Steps-7,result was not equal
Steps-leading-lone-slash-13,Parse error
string-constructor-001,Parse error
string-constructor-002,Parse error
string-constructor-003,Parse error
string-constructor-004,Parse error
string-constructor-005,Parse error
string-constructor-006,Parse error
string-constructor-007,Parse error
string-constructor-008,Parse error
string-constructor-009,Parse error
string-constructor-010,Parse error
string-constructor-011,Parse error
string-constructor-012,Parse error
string-constructor-013,Parse error
string-constructor-014,Parse error
string-constructor-015,Parse error
string-constructor-016,Parse error
string-constructor-017,Parse error
string-constructor-018,Parse error
string-constructor-019,Parse error
string-constructor-020,Parse error
string-constructor-021,Parse error
string-constructor-022,Parse error
string-constructor-023,Parse error
string-constructor-024,Parse error
string-constructor-025,Parse error
string-constructor-026,Parse error
string-constructor-910,Parse error
string-constructor-911,Parse error
string-constructor-912,Parse error
try-001,Parse error
try-002,Parse error
try-003,Parse error
Expand Down
Loading

0 comments on commit 40e08de

Please sign in to comment.