links-notation 0.9.0 → 0.11.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,16 @@
1
+ import { parse } from '../src/parser-generated.js';
2
+
3
+ const withLeading = ` A: a
4
+ B: b`;
5
+
6
+ console.log('Parsing with leading spaces:');
7
+ console.log(withLeading);
8
+ console.log('---');
9
+
10
+ try {
11
+ const result = parse(withLeading);
12
+ console.log('Result:', JSON.stringify(result, null, 2));
13
+ } catch (e) {
14
+ console.log('Error:', e.message);
15
+ console.log(e);
16
+ }
@@ -0,0 +1,33 @@
1
+ import { Parser } from '../src/Parser.js';
2
+
3
+ const parser = new Parser();
4
+
5
+ // Example with 2 leading spaces
6
+ const withLeading = ` A: a
7
+ B: b`;
8
+
9
+ // Example without leading spaces
10
+ const withoutLeading = `A: a
11
+ B: b`;
12
+
13
+ console.log('=== With Leading Spaces ===');
14
+ try {
15
+ const result = parser.parse(withLeading);
16
+ console.log(`Parsed ${result.length} links:`);
17
+ result.forEach((link, i) => {
18
+ console.log(` Link ${i}: ${link.toString()}`);
19
+ });
20
+ } catch (e) {
21
+ console.log('Error:', e.message);
22
+ }
23
+
24
+ console.log('\n=== Without Leading Spaces ===');
25
+ try {
26
+ const result = parser.parse(withoutLeading);
27
+ console.log(`Parsed ${result.length} links:`);
28
+ result.forEach((link, i) => {
29
+ console.log(` Link ${i}: ${link.toString()}`);
30
+ });
31
+ } catch (e) {
32
+ console.log('Error:', e.message);
33
+ }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "links-notation",
3
- "version": "0.9.0",
4
- "description": "Lino protocol parser for JavaScript",
3
+ "version": "0.11.2",
4
+ "description": "Links Notation parser for JavaScript",
5
5
  "main": "dist/index.js",
6
6
  "type": "module",
7
7
  "scripts": {
package/src/grammar.pegjs CHANGED
@@ -1,31 +1,49 @@
1
1
  {
2
2
  let indentationStack = [0];
3
-
3
+ let baseIndentation = null;
4
+
5
+ function setBaseIndentation(spaces) {
6
+ if (baseIndentation === null) {
7
+ baseIndentation = spaces.length;
8
+ }
9
+ }
10
+
11
+ function normalizeIndentation(spaces) {
12
+ if (baseIndentation === null) {
13
+ return spaces.length;
14
+ }
15
+ return Math.max(0, spaces.length - baseIndentation);
16
+ }
17
+
4
18
  function pushIndentation(spaces) {
5
- indentationStack.push(spaces.length);
19
+ const normalized = normalizeIndentation(spaces);
20
+ indentationStack.push(normalized);
6
21
  }
7
-
22
+
8
23
  function popIndentation() {
9
24
  if (indentationStack.length > 1) {
10
25
  indentationStack.pop();
11
26
  }
12
27
  }
13
-
28
+
14
29
  function checkIndentation(spaces) {
15
- return spaces.length >= indentationStack[indentationStack.length - 1];
30
+ const normalized = normalizeIndentation(spaces);
31
+ return normalized >= indentationStack[indentationStack.length - 1];
16
32
  }
17
-
33
+
18
34
  function getCurrentIndentation() {
19
35
  return indentationStack[indentationStack.length - 1];
20
36
  }
21
37
  }
22
38
 
23
- document = _ links:links eof { return links; }
24
- / _ eof { return []; }
39
+ document = &{ indentationStack = [0]; baseIndentation = null; return true; } skipEmptyLines links:links _ eof { return links; }
40
+ / &{ indentationStack = [0]; baseIndentation = null; return true; } _ eof { return []; }
41
+
42
+ skipEmptyLines = ([ \t]* [\r\n])*
25
43
 
26
44
  links = fl:firstLine list:line* { popIndentation(); return [fl].concat(list || []); }
27
45
 
28
- firstLine = l:element { return l; }
46
+ firstLine = SET_BASE_INDENTATION l:element { return l; }
29
47
 
30
48
  line = CHECK_INDENTATION l:element { return l; }
31
49
 
@@ -69,7 +87,9 @@ doubleQuotedReference = '"' r:[^"]+ '"' { return r.join(''); }
69
87
 
70
88
  singleQuotedReference = "'" r:[^']+ "'" { return r.join(''); }
71
89
 
72
- PUSH_INDENTATION = spaces:" "* &{ return spaces.length > getCurrentIndentation(); } { pushIndentation(spaces); }
90
+ SET_BASE_INDENTATION = spaces:" "* { setBaseIndentation(spaces); }
91
+
92
+ PUSH_INDENTATION = spaces:" "* &{ return normalizeIndentation(spaces) > getCurrentIndentation(); } { pushIndentation(spaces); }
73
93
 
74
94
  CHECK_INDENTATION = spaces:" "* &{ return checkIndentation(spaces); }
75
95