Attributes within strings

This commit is contained in:
Nikolay Rozhkov 2023-06-18 01:32:45 +03:00
parent b4b535f997
commit f71769e07b
2 changed files with 36 additions and 22 deletions

View File

@ -20,7 +20,7 @@
(?:<<EOF>>|[\n;])+ { return 'EOS'; } // end of statement is ; \n or end of file
\s+ // skip all whitespace
"{" { this.pushState('group'); return 'OPEN_GROUP'; }
<group>"}" { this.popState('group'); return 'CLOSE_GROUP'; }
<group>"}" { this.popState(); return 'CLOSE_GROUP'; }
"[" { this.pushState('attributes'); return 'OPEN_ATTRIBUTES'; }
<attributes>"]" { this.popState(); return 'CLOSE_ATTRIBUTES'; }
<attributes>\w+ { return 'ATTRIBUTE'; }
@ -30,8 +30,12 @@
<value>[\w]+ { this.popState(); return 'VALUE';}
<value>\s+ //skip
<value>\" { this.pushState('string'); return 'OPEN_STRING'; }
<string>\" { this.popState(); return 'CLOSE_STRING'; }
<string>[\w\s]+(?=\") { return 'STRING'; }
<string>(?!\\)\" {
if(this.topState()==='string') this.popState();
if(this.topState()==='value') this.popState();
return 'CLOSE_STRING';
}
<string>([^"\\]|\\\")+ { console.log(this.state); return 'STRING'; }
// TODO: check if jison will return 2 separate tokens (for nodes) while ignoring whitespace
@ -60,12 +64,9 @@ line
node_with_attributes: NODE OPEN_ATTRIBUTES attributes CLOSE_ATTRIBUTES;
attributes: attribute attributes | ;
attribute: ATTRIBUTE EQUAL VALUE | ATTRIBUTE;
attribute: ATTRIBUTE EQUAL value | ATTRIBUTE;
// flow
// : NODE ARROW value_or_values_group ARROW flow
// | NODE
// ;
value: VALUE | OPEN_STRING STRING CLOSE_STRING;
flow: n_chain_a;

View File

@ -38,22 +38,35 @@ describe('Sankey diagram', function () {
parser.parse(str);
});
it('recognizes a separate node with its attributes', () => {
const str = `
sankey
node[]
node[attr=1]
node[attr=2]
a -> 30 -> b
node[attrWithoutValue]
node[attr = 3]
node[attr1 = 23413 attr2=1234]
node[x1dfqowie attr1 = 23413 attr2]
`;
parser.parse(str);
describe('while attributes parsing', () => {
it('parses different quotless variations', () => {
const str = `
sankey
node[]
node[attr=1]
a -> 30 -> b
node[attrWithoutValue]
node[attr = 3]
node[attr1 = 23413 attr2=1234]
node[x1dfqowie attr1 = 23413 attr2]
`;
parser.parse(str);
});
it('parses strings as values', () => {
const str = `
sankey
node[attr="hello, how are you?"]
node[attr="hello\\""]
`;
parser.parse(str);
});
});
// it('recognizes grouped values', () => {
// const str = `
// sankey