Recognizing attributes

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

View File

@ -2,30 +2,36 @@
%lex
%options case-insensitive
%options easy_keyword_rules
%s group
// when we are inside [] section we are defining attrubutes
%x attributes
%x attribute
%x value
// after attr= we are expecting a value without quotes
%x value
// or if we use "" we are expecting a string containing value
%x string
%%
"sankey" return 'SANKEY'
\d+ return 'AMOUNT'
"->" return 'ARROW'
\w+ return 'NODE'
(?:<<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'; }
"[" { this.pushState('attributes'); return 'OPEN_ATTRIBUTES'; }
<attributes>"]" { this.popState(); return 'CLOSE_ATTRIBUTES'; }
<attributes>\w+ { return 'ATTRIBUTE'; } // string followed by = sign is "attrName"
<attributes>(?=\=s*)[\s\w] {return 'VALUE';}
<attributes>\= { this.pushState('attribute'); return 'EQUAL'; }
<attributes>\s+ // skip all whitespace
<attribute>[\w]+ {this.popState(); return 'VALUE';}
<attribute>\s+ //skip
<attribute>\" { this.pushState('value'); return 'OPEN_VALUE'; }
<value>\" { this.popState(); return 'CLOSE_VALUE'; }
"sankey" { return 'SANKEY'; }
\d+ { return 'AMOUNT'; }
"->" { return 'ARROW'; }
\w+ { return 'NODE'; }
(?:<<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'; }
"[" { this.pushState('attributes'); return 'OPEN_ATTRIBUTES'; }
<attributes>"]" { this.popState(); return 'CLOSE_ATTRIBUTES'; }
<attributes>\w+ { return 'ATTRIBUTE'; }
<attributes>(?=\=s*)[\s\w] { return 'VALUE';}
<attributes>\= { this.pushState('value'); return 'EQUAL'; }
<attributes>\s+ // skip all whitespace
<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'; }
// TODO: check if jison will return 2 separate tokens (for nodes) while ignoring whitespace

View File

@ -41,11 +41,14 @@ describe('Sankey diagram', function () {
it('recognizes a separate node with its attributes', () => {
const str = `
sankey
a[]
b[attr=1]
c[attr=2]
d[attrWithoutValue]
d[attr = 3]
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);