Recognizing attrubutes

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

View File

@ -3,20 +3,29 @@
%options case-insensitive %options case-insensitive
%s group %s group
// %x attributes %x attributes
%x attribute
%x value
%% %%
"{" { this.pushState('group'); return 'OPEN_GROUP'; }
<group>"}" { this.popState('group'); return 'CLOSE_GROUP'; }
"sankey" return 'SANKEY' "sankey" return 'SANKEY'
\d+ return 'VALUE' \d+ return 'AMOUNT'
"->" return 'ARROW' "->" return 'ARROW'
\w+ return 'NODE' \w+ return 'NODE'
"[" {/*this.pushState('attributes');*/ return 'OPEN_ATTRIBUTES'; } (?:<<EOF>>|[\n;])+ { return 'EOS'; } // end of statement is ; \n or end of file
"]" { /* this.popState(); */ return 'CLOSE_ATTRIBUTES'; }
(<<EOF>>|[\n;])+ return 'EOS' // end of statement
\s+ // skip all whitespace \s+ // skip all whitespace
// [\n]+ return 'NEWLINE'; "{" { 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'; }
// TODO: check if jison will return 2 separate tokens (for nodes) while ignoring whitespace // TODO: check if jison will return 2 separate tokens (for nodes) while ignoring whitespace
@ -33,41 +42,26 @@ start
document document
: line document : line document
| // empty |
; ;
line line
// : node_with_attributes // one node with attributes
: flow EOS : flow EOS
| node_with_attributes EOS | node_with_attributes EOS
| EOS | EOS
; ;
node_with_attributes: NODE OPEN_ATTRIBUTES attributes CLOSE_ATTRIBUTES;
node_with_attributes attributes: attribute attributes | ;
: NODE attribute: ATTRIBUTE EQUAL VALUE | ATTRIBUTE;
| NODE attributes_group
;
attributes_group // flow
: OPEN_ATTRIBUTES attributes CLOSE_ATTRIBUTES // : NODE ARROW value_or_values_group ARROW flow
; // | NODE
// ;
attributes: flow: n_chain_a;
| // TODO
;
flow n_chain_a: NODE ARROW a_chain_n | NODE;
: NODE ARROW value_or_values_group ARROW flow a_chain_n: AMOUNT ARROW n_chain_a | AMOUNT;
| NODE
;
value_or_values_group
: OPEN_GROUP values CLOSE_GROUP
| VALUE
;
values
: values VALUE
| /* empty */
;

View File

@ -30,18 +30,9 @@ describe('Sankey diagram', function () {
it('recognizes multiple flows', () => { it('recognizes multiple flows', () => {
const str = ` const str = `
sankey sankey
a -> 30 -> b -> 12 -> e a -> 30 -> b -> 12 -> e;
c -> 30 -> d -> 12 -> e c -> 30 -> d -> 12 -> e;
c -> 40 -> e -> 12 -> e c -> 40 -> e -> 12 -> q;
`;
parser.parse(str);
});
it('recognizes grouped values', () => {
const str = `
sankey
a -> {30} -> b
`; `;
parser.parse(str); parser.parse(str);
@ -50,12 +41,27 @@ describe('Sankey diagram', function () {
it('recognizes a separate node with its attributes', () => { it('recognizes a separate node with its attributes', () => {
const str = ` const str = `
sankey sankey
c[] a[]
b[attr=1]
c[attr=2]
d[attrWithoutValue]
d[attr = 3]
`; `;
parser.parse(str); parser.parse(str);
}); });
// it('recognizes grouped values', () => {
// const str = `
// sankey
// a -> {30} -> b
// `;
// parser.parse(str);
// });
// it('recognizes intake group', () => { // it('recognizes intake group', () => {
// const str = ` // const str = `
// sankey // sankey