Sankey syntax has beed reduced

This commit is contained in:
Nikolay Rozhkov
2023-06-22 15:56:56 +03:00
parent 272615e580
commit be9cd480aa
5 changed files with 39 additions and 34 deletions

View File

@@ -5,6 +5,7 @@ describe('Sankey Diagram', () => {
imgSnapshotTest(
`
sankey
a,b,10
`,
{}

View File

@@ -1,5 +1,7 @@
Agricultural 'waste',Bio-conversion,124.729
%% there are leading and trailing spaces, do not remove
Agricultural 'waste',Bio-conversion,124.729
Bio-conversion,Liquid,0.597
%% line with comment
Bio-conversion,Losses,26.862
Bio-conversion,Solid,280.322
Bio-conversion,Gas,81.144
@@ -50,6 +52,9 @@ Oil reserves,Oil,107.703
Oil,Liquid,611.99
Other waste,Solid,56.587
Other waste,Bio-conversion,77.81
%% blank lines in the middle
Pumped heat,Heating and cooling - homes,193.026
Pumped heat,Heating and cooling - commercial,70.672
Solar PV,Electricity grid,59.901
@@ -66,3 +71,6 @@ Tidal,Electricity grid,9.452
UK land based bioenergy,Bio-conversion,182.01
Wave,Electricity grid,19.013
Wind,Electricity grid,289.366
%% lines at the end, do not remove
1 Agricultural 'waste' %% there are leading and trailing spaces, do not remove Bio-conversion 124.729
2 Agricultural 'waste',Bio-conversion,124.729
3 Bio-conversion Bio-conversion,Liquid,0.597 Liquid 0.597
4 %% line with comment
5 Bio-conversion Bio-conversion,Losses,26.862 Losses 26.862
6 Bio-conversion Bio-conversion,Solid,280.322 Solid 280.322
7 Bio-conversion Bio-conversion,Gas,81.144 Gas 81.144
52 Oil Oil,Liquid,611.99 Liquid 611.99
53 Other waste Other waste,Solid,56.587 Solid 56.587
54 Other waste Other waste,Bio-conversion,77.81 Bio-conversion 77.81
55 %% blank lines in the middle
56 Pumped heat,Heating and cooling - homes,193.026
57 Pumped heat,Heating and cooling - commercial,70.672
58 Pumped heat Solar PV,Electricity grid,59.901 Heating and cooling - homes 193.026
59 Pumped heat Solar Thermal,Heating and cooling - homes,19.263 Heating and cooling - commercial 70.672
60 Solar PV Solar,Solar Thermal,19.263 Electricity grid 59.901
71 UK land based bioenergy Wind,Electricity grid,289.366 Bio-conversion 182.01
72 Wave %% lines at the end, do not remove Electricity grid 19.013
73 Wind Electricity grid 289.366
74
75
76

View File

@@ -23,10 +23,10 @@ TEXTDATA [\u0020-\u0021\u0023-\u002B\u002D-\u007E]
%%
<<EOF>> { return 'EOF' }
<<EOF>> { return 'EOF' } // match end of file
"sankey" { return 'SANKEY' }
({CRLF}|{LF})+ { return 'NEWLINE' } // let newline to be multiple lines
({CRLF}|{LF}) { return 'NEWLINE' }
{COMMA} { return 'COMMA' }
{DQUOTE} { return 'DQUOTE' }
{TEXTDATA}* { return 'NON_ESCAPED_TEXT' }
@@ -38,28 +38,11 @@ TEXTDATA [\u0020-\u0021\u0023-\u002B\u002D-\u007E]
%% // language grammar
start
: SANKEY csv opt_eof
;
start: SANKEY NEWLINE csv opt_eof;
csv
: record csv_tail
;
csv_tail
: NEWLINE csv
| // empty
;
opt_newline
: NEWLINE
| // empty
;
opt_eof
: EOF
| // empty
;
csv: record csv_tail;
csv_tail: NEWLINE csv | ;
opt_eof: EOF | ;
record
: field\[source] COMMA field\[target] COMMA field\[value] {
@@ -68,7 +51,6 @@ record
const value = parseFloat($value.trim());
$$ = yy.addLink(source,target,value);
} // parse only 3 fields, this is not part of CSV standard
| {} // allow empty record to handle empty lines, this is not part of CSV standard either
;
field

View File

@@ -3,7 +3,8 @@ import diagram from './sankey.jison';
// @ts-ignore: jison doesn't export types
import { parser } from './sankey.jison';
import db from '../sankeyDB.js';
// import { fail } from 'assert';
import { cleanupComments } from '../../../diagram-api/comments.js';
import { prepareTextForParsing } from '../sankeyDiagram.js';
describe('Sankey diagram', function () {
// TODO - these examples should be put into ./parser/stateDiagram.spec.js
@@ -18,15 +19,16 @@ describe('Sankey diagram', function () {
const fs = await import('fs');
const path = await import('path');
const csv = path.resolve(__dirname, './energy.csv');
fs.readFile(csv, 'utf8', (err: NodeJS.ErrnoException | null, data: string) => {
if (err) {
throw err;
}
const data = fs.readFileSync(csv, 'utf8');
const str = `sankey\\n${data}`;
// Add \n\n + space to emulate possible possible imperfections
const graphDefinition = prepareTextForParsing(cleanupComments('sankey\n\n ' + data));
// const textToParse = graphDefinition
// .replaceAll(/^[^\S\r\n]+|[^\S\r\n]+$/g, '')
// .replaceAll(/([\n\r])+/g, "\n")
// .trim();
parser.parse(str);
});
parser.parse(graphDefinition);
});
});
});

View File

@@ -5,6 +5,18 @@ import db from './sankeyDB.js';
import styles from './styles.js';
import renderer from './sankeyRenderer.js';
export const prepareTextForParsing = (text: string): string => {
const textToParse = text
.replaceAll(/^[^\S\r\n]+|[^\S\r\n]+$/g, '') // remove all trailing spaces for each row
.replaceAll(/([\n\r])+/g, '\n') // remove empty lines duplicated
.trim();
return textToParse;
};
const originalParse = parser.parse.bind(parser);
parser.parse = (text: string) => originalParse(prepareTextForParsing(text));
export const diagram: DiagramDefinition = {
parser,
db,