n3 2.2.13 → 2.2.14

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.
package/lib/N3Parser.js CHANGED
@@ -86,34 +86,39 @@ class N3Parser {
86
86
  // ### `_saveContext` stores the current parsing context
87
87
  // when entering a new scope (list, blank node, formula)
88
88
  _saveContext(type, graph, subject, predicate, object) {
89
- const n3Mode = this._n3Mode;
89
+ // Only N3 contexts need the extra parser state.
90
+ if (!this._n3Mode) {
91
+ this._contextStack.push({
92
+ type,
93
+ subject,
94
+ predicate,
95
+ object,
96
+ graph
97
+ });
98
+ return;
99
+ }
90
100
  this._contextStack.push({
91
101
  type,
92
102
  subject,
93
103
  predicate,
94
104
  object,
95
105
  graph,
96
- inverse: n3Mode ? this._inversePredicate : false,
97
- blankPrefix: n3Mode ? this._prefixes._ : '',
98
- quantified: n3Mode ? this._quantified : null,
99
- emptyFormula: n3Mode ? this._emptyFormula : false
106
+ inverse: this._inversePredicate,
107
+ blankPrefix: this._prefixes._,
108
+ quantified: this._quantified,
109
+ emptyFormula: this._emptyFormula
100
110
  });
101
- // The settings below only apply to N3 streams
102
- if (n3Mode) {
103
- // Every new scope resets the predicate direction
104
- this._inversePredicate = false;
105
- // In N3, blank nodes are scoped to a formula
106
- // (using a dot as separator, as a blank node label cannot start with it)
107
- this._prefixes._ = this._graph ? `${this._graph.value}.` : '.';
108
- // Quantifiers are scoped to a formula
109
- this._quantified = Object.create(this._quantified);
110
- // A formula starts a new statement, so the subject of the
111
- // enclosing statement must not leak into it,
112
- // and it is empty until a statement has been read
113
- if (type === 'formula') {
114
- this._subject = null;
115
- this._emptyFormula = true;
116
- }
111
+ // Every new scope resets the predicate direction
112
+ this._inversePredicate = false;
113
+ // In N3, blank nodes are scoped to a formula
114
+ // (using a dot as separator, as a blank node label cannot start with it)
115
+ this._prefixes._ = this._graph ? `${this._graph.value}.` : '.';
116
+ // Quantifiers are scoped to a formula
117
+ this._quantified = Object.create(this._quantified);
118
+ // A formula starts empty and must not inherit its parent's subject
119
+ if (type === 'formula') {
120
+ this._subject = null;
121
+ this._emptyFormula = true;
117
122
  }
118
123
  }
119
124
 
@@ -595,10 +600,10 @@ class N3Parser {
595
600
  }
596
601
 
597
602
  // ### `_completeLiteral` completes a literal with an optional datatype or language
603
+ // Defers possible direction tags without allocating bound callbacks.
598
604
  _completeLiteral(token, component) {
599
- // Create a simple string literal by default
600
- let literal = this._factory.literal(this._literalValue);
601
- let readCb;
605
+ let literal,
606
+ readCb = false;
602
607
  switch (token.type) {
603
608
  // Create a datatyped literal
604
609
  case 'type':
@@ -617,8 +622,13 @@ class N3Parser {
617
622
  literal = this._factory.literal(this._literalValue, token.value);
618
623
  this._literalLanguage = token.value;
619
624
  token = null;
620
- readCb = this._readDirCode.bind(this, component);
625
+ // Save state for a possible direction tag
626
+ this._literalComponent = component;
627
+ readCb = true;
621
628
  break;
629
+ // Create a simple string literal by default
630
+ default:
631
+ literal = this._factory.literal(this._literalValue);
622
632
  }
623
633
  return {
624
634
  token,
@@ -626,7 +636,11 @@ class N3Parser {
626
636
  readCb
627
637
  };
628
638
  }
629
- _readDirCode(component, listItem, token) {
639
+
640
+ // ### `_readDirCode` reads an optional directional language tag
641
+ _readDirCode(token) {
642
+ const component = this._literalComponent,
643
+ listItem = this._literalListItem;
630
644
  // Attempt to read a dircode
631
645
  if (token.type === 'dircode') {
632
646
  const term = this._factory.literal(this._literalValue, {
@@ -661,7 +675,10 @@ class N3Parser {
661
675
  }
662
676
 
663
677
  // Postpone completion if the literal is only partially completed (such as lang+dir).
664
- if (completed.readCb) return completed.readCb.bind(this, false);
678
+ if (completed.readCb) {
679
+ this._literalListItem = false;
680
+ return this._readDirCode;
681
+ }
665
682
 
666
683
  // A subject literal implies N3 mode, so it might start a path.
667
684
  if (component === 'subject') {
@@ -691,7 +708,10 @@ class N3Parser {
691
708
  this._object = completed.literal;
692
709
 
693
710
  // Postpone completion if the literal is only partially completed (such as lang+dir).
694
- if (completed.readCb) return completed.readCb.bind(this, listItem);
711
+ if (completed.readCb) {
712
+ this._literalListItem = listItem;
713
+ return this._readDirCode;
714
+ }
695
715
  return this._completeObjectLiteralPost(completed.token, listItem);
696
716
  }
697
717
  _completeObjectLiteralPost(token, listItem) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "2.2.13",
3
+ "version": "2.2.14",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Parser.js CHANGED
@@ -78,31 +78,30 @@ export default class N3Parser {
78
78
  // ### `_saveContext` stores the current parsing context
79
79
  // when entering a new scope (list, blank node, formula)
80
80
  _saveContext(type, graph, subject, predicate, object) {
81
- const n3Mode = this._n3Mode;
81
+ // Only N3 contexts need the extra parser state.
82
+ if (!this._n3Mode) {
83
+ this._contextStack.push({ type, subject, predicate, object, graph });
84
+ return;
85
+ }
82
86
  this._contextStack.push({
83
87
  type,
84
88
  subject, predicate, object, graph,
85
- inverse: n3Mode ? this._inversePredicate : false,
86
- blankPrefix: n3Mode ? this._prefixes._ : '',
87
- quantified: n3Mode ? this._quantified : null,
88
- emptyFormula: n3Mode ? this._emptyFormula : false,
89
+ inverse: this._inversePredicate,
90
+ blankPrefix: this._prefixes._,
91
+ quantified: this._quantified,
92
+ emptyFormula: this._emptyFormula,
89
93
  });
90
- // The settings below only apply to N3 streams
91
- if (n3Mode) {
92
- // Every new scope resets the predicate direction
93
- this._inversePredicate = false;
94
- // In N3, blank nodes are scoped to a formula
95
- // (using a dot as separator, as a blank node label cannot start with it)
96
- this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
97
- // Quantifiers are scoped to a formula
98
- this._quantified = Object.create(this._quantified);
99
- // A formula starts a new statement, so the subject of the
100
- // enclosing statement must not leak into it,
101
- // and it is empty until a statement has been read
102
- if (type === 'formula') {
103
- this._subject = null;
104
- this._emptyFormula = true;
105
- }
94
+ // Every new scope resets the predicate direction
95
+ this._inversePredicate = false;
96
+ // In N3, blank nodes are scoped to a formula
97
+ // (using a dot as separator, as a blank node label cannot start with it)
98
+ this._prefixes._ = (this._graph ? `${this._graph.value}.` : '.');
99
+ // Quantifiers are scoped to a formula
100
+ this._quantified = Object.create(this._quantified);
101
+ // A formula starts empty and must not inherit its parent's subject
102
+ if (type === 'formula') {
103
+ this._subject = null;
104
+ this._emptyFormula = true;
106
105
  }
107
106
  }
108
107
 
@@ -641,10 +640,9 @@ export default class N3Parser {
641
640
  }
642
641
 
643
642
  // ### `_completeLiteral` completes a literal with an optional datatype or language
643
+ // Defers possible direction tags without allocating bound callbacks.
644
644
  _completeLiteral(token, component) {
645
- // Create a simple string literal by default
646
- let literal = this._factory.literal(this._literalValue);
647
- let readCb;
645
+ let literal, readCb = false;
648
646
 
649
647
  switch (token.type) {
650
648
  // Create a datatyped literal
@@ -665,14 +663,21 @@ export default class N3Parser {
665
663
  literal = this._factory.literal(this._literalValue, token.value);
666
664
  this._literalLanguage = token.value;
667
665
  token = null;
668
- readCb = this._readDirCode.bind(this, component);
666
+ // Save state for a possible direction tag
667
+ this._literalComponent = component;
668
+ readCb = true;
669
669
  break;
670
+ // Create a simple string literal by default
671
+ default:
672
+ literal = this._factory.literal(this._literalValue);
670
673
  }
671
674
 
672
675
  return { token, literal, readCb };
673
676
  }
674
677
 
675
- _readDirCode(component, listItem, token) {
678
+ // ### `_readDirCode` reads an optional directional language tag
679
+ _readDirCode(token) {
680
+ const component = this._literalComponent, listItem = this._literalListItem;
676
681
  // Attempt to read a dircode
677
682
  if (token.type === 'dircode') {
678
683
  const term = this._factory.literal(this._literalValue, { language: this._literalLanguage, direction: token.value });
@@ -714,8 +719,10 @@ export default class N3Parser {
714
719
  }
715
720
 
716
721
  // Postpone completion if the literal is only partially completed (such as lang+dir).
717
- if (completed.readCb)
718
- return completed.readCb.bind(this, false);
722
+ if (completed.readCb) {
723
+ this._literalListItem = false;
724
+ return this._readDirCode;
725
+ }
719
726
 
720
727
  // A subject literal implies N3 mode, so it might start a path.
721
728
  if (component === 'subject') {
@@ -748,8 +755,10 @@ export default class N3Parser {
748
755
  this._object = completed.literal;
749
756
 
750
757
  // Postpone completion if the literal is only partially completed (such as lang+dir).
751
- if (completed.readCb)
752
- return completed.readCb.bind(this, listItem);
758
+ if (completed.readCb) {
759
+ this._literalListItem = listItem;
760
+ return this._readDirCode;
761
+ }
753
762
 
754
763
  return this._completeObjectLiteralPost(completed.token, listItem);
755
764
  }