regular-grammar
Advanced tools
Comparing version 0.1.2 to 0.1.4
{ | ||
"name": "regular-grammar", | ||
"version": "0.1.2", | ||
"version": "0.1.4", | ||
"description": "A simple regular grammar/parser generator", | ||
@@ -5,0 +5,0 @@ "author": "Victor Grishchenko <victor.grishchenko@gmail.com>", |
@@ -50,2 +50,6 @@ /** The class generates regex _parsers for simple regular languages | ||
let marker; | ||
let marker_optional = m[1] && m[1].length>1 && m[1][m[1].length-1]==='?' ? true : false; | ||
if (marker_optional) | ||
m[1] = m[1].substr(0,m[1].length-1); | ||
if ( !m[1] ) { | ||
@@ -63,3 +67,2 @@ marker = ''; | ||
marker = re_mrk.source; | ||
console.warn("MARKER", m[2], marker); | ||
} else { | ||
@@ -74,2 +77,3 @@ throw new Error('marker parse fail: '+m); | ||
marker, | ||
marker_optional, | ||
rule, | ||
@@ -98,3 +102,5 @@ quantifier, | ||
if (this._splitters[t.formula]) { return this._splitters[t.formula]; } | ||
let p = (t.marker.length === 1 ? '\\' : '') + t.marker + '\\s*'; | ||
let p = (t.marker.length === 1 ? '\\' : '') + t.marker + | ||
(t.marker_optional?'?':'') + | ||
'\\s*'; | ||
p += '(' + this.pattern(t.rule) + ')'; | ||
@@ -114,2 +120,5 @@ const splitter = new RegExp(p, 'g'); | ||
} | ||
if (t.marker_optional) { | ||
m = m + '?'; | ||
} | ||
if (!t.repeating) { | ||
@@ -204,3 +213,3 @@ if (t.marker.length > 1) { | ||
Grammar.TRIPLET_RE = /(\[\S*?\]|"(?:\\.|[^"])*"|\/([^\/\s]+)\/|[^A-Za-z0-9\s])?([A-Z][A-Z0-9_]*)?([*+?|]|{\d+(?:,\d+)?})?/g; | ||
Grammar.TRIPLET_RE = /((?:\[\S*?\]|"(?:\\.|[^"])*"|\/([^\/\s]+)\/|[^A-Za-z0-9\s])\??)?([A-Z][A-Z0-9_]*)?([*+?|]|{\d+(?:,\d+)?})?/g; | ||
@@ -207,0 +216,0 @@ function sterilize (pattern) { |
@@ -40,2 +40,3 @@ const tap = require('tape'); | ||
marker: '', | ||
marker_optional: false, | ||
rule: 'INT', | ||
@@ -49,2 +50,3 @@ quantifier: '', | ||
marker: '[+%-]', | ||
marker_optional: false, | ||
rule: 'INT', | ||
@@ -51,0 +53,0 @@ quantifier: '?', |
@@ -99,1 +99,16 @@ const tape = require('tape'); | ||
}); | ||
tape('grammar.03.F OPTIONAL MARKER', (tap) => { | ||
const WORDS = new Grammar({ | ||
WORD: /\w+/, | ||
WORDS: ",?WORD+" | ||
}); | ||
tap.ok(WORDS.is("some words, more words", "WORDS")); | ||
const words = WORDS.split("some words, more words", "WORDS"); | ||
tap.deepEqual(words, [["some", "words", "more", "words"]] ); | ||
console.warn(words); | ||
tap.end(); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
52440
473