Skip to content

Commit

Permalink
Update grammar to strip whitespace using Ohm matcher
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesmengo committed Jan 14, 2025
1 parent 9edbe0a commit ba370d4
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 61 deletions.
15 changes: 11 additions & 4 deletions packages/liquid-html-parser/grammar/liquid-html.ohm
Original file line number Diff line number Diff line change
Expand Up @@ -394,11 +394,18 @@ LiquidDoc <: Helpers {
| paramNode
| fallbackNode

fallbackNode = "@" anyExceptStar<newline>
paramNode = "@param" space* paramType? space* paramName (space* "-")? paramDescription
paramType = "{" anyExceptStar<"}"> "}"
//By default, space matches new lines as well. We override it here to make writing rules easier.
strictSpace = " " | "\t"
// We use this as an escape hatch to stop matching TextNode and try again when one of these characters is encountered
openControl:= "@" | end

fallbackNode = "@" anyExceptStar<endOfParam>
paramNode = "@param" strictSpace* paramType? strictSpace* paramName (strictSpace* "-")? strictSpace* paramDescription
paramType = "{" strictSpace* paramTypeContent strictSpace* "}"
paramTypeContent = anyExceptStar<("}"| strictSpace)>
paramName = identifierCharacter+
paramDescription = anyExceptStar<(newline | end)>
paramDescription = anyExceptStar<endOfParam>
endOfParam = strictSpace* (newline | end)
}

LiquidHTML <: Liquid {
Expand Down
12 changes: 5 additions & 7 deletions packages/liquid-html-parser/src/stage-1-cst.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1048,13 +1048,13 @@ describe('Unit: Stage 1 (CST)', () => {

expectPath(cst, '0.children.0.paramType.type').to.equal('TextNode');
expectPath(cst, '0.children.0.paramType.value').to.equal('String');
expectPath(cst, '0.children.0.paramType.locStart').to.equal(testStr.indexOf('{String}'));
expectPath(cst, '0.children.0.paramType.locStart').to.equal(testStr.indexOf('String'));
expectPath(cst, '0.children.0.paramType.locEnd').to.equal(
testStr.indexOf('{String}') + '{String}'.length,
testStr.indexOf('String') + 'String'.length,
);
});

it('should parse @param with type with space inside', () => {
it('should strip whitespace around param type for @param annotation', () => {
const testStr = `{% doc %} @param { String } paramWithType {% enddoc %}`;
cst = toCST(testStr);

Expand All @@ -1063,11 +1063,9 @@ describe('Unit: Stage 1 (CST)', () => {

expectPath(cst, '0.children.0.paramType.type').to.equal('TextNode');
expectPath(cst, '0.children.0.paramType.value').to.equal('String');
expectPath(cst, '0.children.0.paramType.locStart').to.equal(
testStr.indexOf('{ String }'),
);
expectPath(cst, '0.children.0.paramType.locStart').to.equal(testStr.indexOf('String'));
expectPath(cst, '0.children.0.paramType.locEnd').to.equal(
testStr.indexOf('{ String }') + '{ String }'.length,
testStr.indexOf('String') + 'String'.length,
);
});

Expand Down
73 changes: 23 additions & 50 deletions packages/liquid-html-parser/src/stage-1-cst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -441,15 +441,14 @@ export interface ConcreteYamlFrontmatterNode

export type LiquidHtmlConcreteNode =
| ConcreteHtmlNode
| ConcreteLiquidNode
| ConcreteTextNode
| ConcreteYamlFrontmatterNode
| LiquidDocConcreteNode;
| LiquidConcreteNode;

export type LiquidConcreteNode =
| ConcreteLiquidNode
| ConcreteTextNode
| ConcreteYamlFrontmatterNode;
| ConcreteYamlFrontmatterNode
| LiquidDocConcreteNode;

export type LiquidHtmlCST = LiquidHtmlConcreteNode[];

Expand Down Expand Up @@ -1317,17 +1316,22 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
throw new LiquidHTMLCSTParsingError(res);
}

/**
* Reusable text node type
*/
const textNode = {
type: ConcreteNodeTypes.TextNode,
value: function () {
return (this as any).sourceString;
},
locStart,
locEnd,
source,
};

const LiquidDocMappings: Mapping = {
Node: 0,
TextNode: {
type: ConcreteNodeTypes.TextNode,
value: function () {
return (this as any).sourceString;
},
locStart,
locEnd,
source,
},
TextNode: textNode,
paramNode: {
type: ConcreteNodeTypes.LiquidDocParamNode,
name: 'param',
Expand All @@ -1336,44 +1340,13 @@ function toLiquidDocAST(source: string, matchingSource: string, offset: number)
source,
paramType: 2,
paramName: 4,
paramDescription: 7,
},
paramType: {
type: ConcreteNodeTypes.TextNode,
value: function (nodes: Node[]) {
return nodes[1].sourceString.trim();
},
source,
locStart,
locEnd,
},
paramName: {
type: ConcreteNodeTypes.TextNode,
value: function (nodes: Node[]) {
return nodes[0].sourceString.trim();
},
source,
locStart,
locEnd,
},
paramDescription: {
type: ConcreteNodeTypes.TextNode,
value: function (nodes: Node[]) {
return nodes[0].sourceString.trim();
},
source,
locStart,
locEnd,
},
fallbackNode: {
type: ConcreteNodeTypes.TextNode,
value: function () {
return (this as any).sourceString.trim();
},
locStart,
locEnd,
source,
paramDescription: 8,
},
paramType: 2,
paramTypeContent: textNode,
paramName: textNode,
paramDescription: textNode,
fallbackNode: textNode,
};

return toAST(res, LiquidDocMappings);
Expand Down

0 comments on commit ba370d4

Please sign in to comment.