@emmetio/math-expression
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -5,125 +5,6 @@ 'use strict'; | ||
/** | ||
* Check if given code is a number | ||
*/ | ||
function isNumber(code) { | ||
return code > 47 && code < 58; | ||
} | ||
/** | ||
* Check if given character code is a white-space character: a space character | ||
* or line breaks | ||
*/ | ||
function isWhiteSpace(code) { | ||
return code === 32 /* space */ | ||
|| code === 9 /* tab */ | ||
|| code === 160; /* non-breaking space */ | ||
} | ||
/** | ||
* Check if given character code is a space character | ||
*/ | ||
function isSpace(code) { | ||
return isWhiteSpace(code) | ||
|| code === 10 /* LF */ | ||
|| code === 13; /* CR */ | ||
} | ||
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } | ||
/** | ||
* A streaming, character code-based string reader | ||
*/ | ||
class Scanner { | ||
constructor(str, start, end) { | ||
if (end == null && typeof str === 'string') { | ||
end = str.length; | ||
} | ||
this.string = str; | ||
this.pos = this.start = start || 0; | ||
this.end = end || 0; | ||
} | ||
/** | ||
* Returns true only if the stream is at the end of the file. | ||
*/ | ||
eof() { | ||
return this.pos >= this.end; | ||
} | ||
/** | ||
* Creates a new stream instance which is limited to given `start` and `end` | ||
* range. E.g. its `eof()` method will look at `end` property, not actual | ||
* stream end | ||
*/ | ||
limit(start, end) { | ||
return new Scanner(this.string, start, end); | ||
} | ||
/** | ||
* Returns the next character code in the stream without advancing it. | ||
* Will return NaN at the end of the file. | ||
*/ | ||
peek() { | ||
return this.string.charCodeAt(this.pos); | ||
} | ||
/** | ||
* Returns the next character in the stream and advances it. | ||
* Also returns <code>undefined</code> when no more characters are available. | ||
*/ | ||
next() { | ||
if (this.pos < this.string.length) { | ||
return this.string.charCodeAt(this.pos++); | ||
} | ||
} | ||
/** | ||
* `match` can be a character code or a function that takes a character code | ||
* and returns a boolean. If the next character in the stream 'matches' | ||
* the given argument, it is consumed and returned. | ||
* Otherwise, `false` is returned. | ||
*/ | ||
eat(match) { | ||
const ch = this.peek(); | ||
const ok = typeof match === 'function' ? match(ch) : ch === match; | ||
if (ok) { | ||
this.next(); | ||
} | ||
return ok; | ||
} | ||
/** | ||
* Repeatedly calls <code>eat</code> with the given argument, until it | ||
* fails. Returns <code>true</code> if any characters were eaten. | ||
*/ | ||
eatWhile(match) { | ||
const start = this.pos; | ||
while (!this.eof() && this.eat(match)) { /* */ } | ||
return this.pos !== start; | ||
} | ||
/** | ||
* Backs up the stream n characters. Backing it up further than the | ||
* start of the current token will cause things to break, so be careful. | ||
*/ | ||
backUp(n) { | ||
this.pos -= (n || 1); | ||
} | ||
/** | ||
* Get the string between the start of the current token and the | ||
* current stream position. | ||
*/ | ||
current() { | ||
return this.substring(this.start, this.pos); | ||
} | ||
/** | ||
* Returns substring for given range | ||
*/ | ||
substring(start, end) { | ||
return this.string.slice(start, end); | ||
} | ||
/** | ||
* Creates error object with current stream state | ||
*/ | ||
error(message, pos = this.pos) { | ||
return new ScannerError(`${message} at ${pos + 1}`, pos, this.string); | ||
} | ||
} | ||
class ScannerError extends Error { | ||
constructor(message, pos, str) { | ||
super(message); | ||
this.pos = pos; | ||
this.string = str; | ||
} | ||
} | ||
var Scanner = require('@emmetio/scanner'); | ||
var Scanner__default = _interopDefault(Scanner); | ||
@@ -135,3 +16,3 @@ const nullary = token("null" /* Null */, 0); | ||
function parse(expr) { | ||
const scanner = typeof expr === 'string' ? new Scanner(expr) : expr; | ||
const scanner = typeof expr === 'string' ? new Scanner__default(expr) : expr; | ||
let ch; | ||
@@ -142,3 +23,3 @@ let priority = 0; | ||
while (!scanner.eof()) { | ||
scanner.eatWhile(isWhiteSpace); | ||
scanner.eatWhile(Scanner.isWhiteSpace); | ||
scanner.start = scanner.pos; | ||
@@ -204,7 +85,7 @@ if (consumeNumber(scanner)) { | ||
const start = scanner.pos; | ||
if (scanner.eat(46 /* Dot */) && scanner.eatWhile(isNumber)) { | ||
if (scanner.eat(46 /* Dot */) && scanner.eatWhile(Scanner.isNumber)) { | ||
// short decimal notation: .025 | ||
return true; | ||
} | ||
if (scanner.eatWhile(isNumber) && (!scanner.eat(46 /* Dot */) || scanner.eatWhile(isNumber))) { | ||
if (scanner.eatWhile(Scanner.isNumber) && (!scanner.eat(46 /* Dot */) || scanner.eatWhile(Scanner.isNumber))) { | ||
// either integer or decimal: 10, 10.25 | ||
@@ -279,3 +160,3 @@ return true; | ||
if (scanner) { | ||
name += ` at column ${scanner.start} of expression`; | ||
name += ` at column ${scanner.pos} of expression`; | ||
} | ||
@@ -315,3 +196,3 @@ throw new Error(name); | ||
ch = cur(scanner); | ||
if (ch !== 41 /* RightParenthesis */ && !(opt.whitespace && isSpace(ch))) { | ||
if (ch !== 41 /* RightParenthesis */ && !(opt.whitespace && Scanner.isSpace(ch))) { | ||
break; | ||
@@ -338,3 +219,3 @@ } | ||
} | ||
else if (!((opt.whitespace && isSpace(ch)) || isSign(ch) || isOperator(ch))) { | ||
else if (!((opt.whitespace && Scanner.isSpace(ch)) || isSign(ch) || isOperator(ch))) { | ||
break; | ||
@@ -346,3 +227,3 @@ } | ||
// Trim whitespace | ||
while (isSpace(cur(scanner))) { | ||
while (Scanner.isSpace(cur(scanner))) { | ||
scanner.pos++; | ||
@@ -358,3 +239,3 @@ } | ||
function number$1(scanner) { | ||
if (isNumber(prev(scanner))) { | ||
if (Scanner.isNumber(prev(scanner))) { | ||
scanner.pos--; | ||
@@ -372,3 +253,3 @@ let dot = false; | ||
} | ||
else if (!isNumber(ch)) { | ||
else if (!Scanner.isNumber(ch)) { | ||
break; | ||
@@ -375,0 +256,0 @@ } |
@@ -1,125 +0,3 @@ | ||
/** | ||
* Check if given code is a number | ||
*/ | ||
function isNumber(code) { | ||
return code > 47 && code < 58; | ||
} | ||
/** | ||
* Check if given character code is a white-space character: a space character | ||
* or line breaks | ||
*/ | ||
function isWhiteSpace(code) { | ||
return code === 32 /* space */ | ||
|| code === 9 /* tab */ | ||
|| code === 160; /* non-breaking space */ | ||
} | ||
/** | ||
* Check if given character code is a space character | ||
*/ | ||
function isSpace(code) { | ||
return isWhiteSpace(code) | ||
|| code === 10 /* LF */ | ||
|| code === 13; /* CR */ | ||
} | ||
import Scanner, { isWhiteSpace, isNumber, isSpace } from '@emmetio/scanner'; | ||
/** | ||
* A streaming, character code-based string reader | ||
*/ | ||
class Scanner { | ||
constructor(str, start, end) { | ||
if (end == null && typeof str === 'string') { | ||
end = str.length; | ||
} | ||
this.string = str; | ||
this.pos = this.start = start || 0; | ||
this.end = end || 0; | ||
} | ||
/** | ||
* Returns true only if the stream is at the end of the file. | ||
*/ | ||
eof() { | ||
return this.pos >= this.end; | ||
} | ||
/** | ||
* Creates a new stream instance which is limited to given `start` and `end` | ||
* range. E.g. its `eof()` method will look at `end` property, not actual | ||
* stream end | ||
*/ | ||
limit(start, end) { | ||
return new Scanner(this.string, start, end); | ||
} | ||
/** | ||
* Returns the next character code in the stream without advancing it. | ||
* Will return NaN at the end of the file. | ||
*/ | ||
peek() { | ||
return this.string.charCodeAt(this.pos); | ||
} | ||
/** | ||
* Returns the next character in the stream and advances it. | ||
* Also returns <code>undefined</code> when no more characters are available. | ||
*/ | ||
next() { | ||
if (this.pos < this.string.length) { | ||
return this.string.charCodeAt(this.pos++); | ||
} | ||
} | ||
/** | ||
* `match` can be a character code or a function that takes a character code | ||
* and returns a boolean. If the next character in the stream 'matches' | ||
* the given argument, it is consumed and returned. | ||
* Otherwise, `false` is returned. | ||
*/ | ||
eat(match) { | ||
const ch = this.peek(); | ||
const ok = typeof match === 'function' ? match(ch) : ch === match; | ||
if (ok) { | ||
this.next(); | ||
} | ||
return ok; | ||
} | ||
/** | ||
* Repeatedly calls <code>eat</code> with the given argument, until it | ||
* fails. Returns <code>true</code> if any characters were eaten. | ||
*/ | ||
eatWhile(match) { | ||
const start = this.pos; | ||
while (!this.eof() && this.eat(match)) { /* */ } | ||
return this.pos !== start; | ||
} | ||
/** | ||
* Backs up the stream n characters. Backing it up further than the | ||
* start of the current token will cause things to break, so be careful. | ||
*/ | ||
backUp(n) { | ||
this.pos -= (n || 1); | ||
} | ||
/** | ||
* Get the string between the start of the current token and the | ||
* current stream position. | ||
*/ | ||
current() { | ||
return this.substring(this.start, this.pos); | ||
} | ||
/** | ||
* Returns substring for given range | ||
*/ | ||
substring(start, end) { | ||
return this.string.slice(start, end); | ||
} | ||
/** | ||
* Creates error object with current stream state | ||
*/ | ||
error(message, pos = this.pos) { | ||
return new ScannerError(`${message} at ${pos + 1}`, pos, this.string); | ||
} | ||
} | ||
class ScannerError extends Error { | ||
constructor(message, pos, str) { | ||
super(message); | ||
this.pos = pos; | ||
this.string = str; | ||
} | ||
} | ||
const nullary = token("null" /* Null */, 0); | ||
@@ -271,3 +149,3 @@ /** | ||
if (scanner) { | ||
name += ` at column ${scanner.start} of expression`; | ||
name += ` at column ${scanner.pos} of expression`; | ||
} | ||
@@ -274,0 +152,0 @@ throw new Error(name); |
{ | ||
"name": "@emmetio/math-expression", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Parse and evaluate simple math expressions", | ||
@@ -32,13 +32,15 @@ "main": "./dist/math.cjs.js", | ||
"homepage": "https://github.com/emmetio/math-expression#readme", | ||
"dependencies": { | ||
"@emmetio/scanner": "0.0.7" | ||
}, | ||
"devDependencies": { | ||
"@emmetio/scanner": "0.0.7", | ||
"@types/mocha": "^5.2.7", | ||
"@types/mocha": "^7.0.2", | ||
"@types/node": "^12.7.12", | ||
"mocha": "^6.2.1", | ||
"rollup": "^1.24.0", | ||
"mocha": "^7.1.1", | ||
"rollup": "^2.6.0", | ||
"rollup-plugin-node-resolve": "^5.2.0", | ||
"rollup-plugin-typescript2": "^0.24.3", | ||
"ts-node": "^8.4.1", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.4" | ||
"rollup-plugin-typescript2": "^0.27.0", | ||
"ts-node": "^8.8.2", | ||
"tslint": "^6.1.1", | ||
"typescript": "^3.8.3" | ||
}, | ||
@@ -45,0 +47,0 @@ "mocha": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
9
62313
1
662
+ Added@emmetio/scanner@0.0.7
+ Added@emmetio/scanner@0.0.7(transitive)