@desertnet/scanner
Advanced tools
Comparing version
{ | ||
"name": "@desertnet/scanner", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "A regex-based string scanner/tokenizer", | ||
"main": "index.js", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"test": "mocha --compilers js:babel-register", | ||
"build": "rimraf ./dist && babel -d ./dist/es5 -s inline ./lib", | ||
"preversion": "npm test", | ||
"prepublish": "npm run build" | ||
"build": "rimraf dist && babel -s inline -D src -d dist", | ||
"clean": "rimraf dist coverage .nyc_output", | ||
"lint": "eslint src test", | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm test", | ||
"preversion": "npm test && npm run lint", | ||
"test": "cross-env NODE_ENV=test mocha --require @babel/register --throw-deprecation", | ||
"test:coverage": "cross-env NODE_ENV=test nyc mocha", | ||
"test:watch": "chokidar src test mock fixtures --initial -c 'npm t'" | ||
}, | ||
@@ -21,3 +26,3 @@ "repository": { | ||
"author": "Eric McCarthy <eric@limulus.net> (http://www.limulus.net/)", | ||
"license": "SEE LICENSE IN LICENSE", | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -28,9 +33,16 @@ "url": "https://github.com/desertnet/scanner/issues" | ||
"devDependencies": { | ||
"babel-cli": "^6.11.4", | ||
"babel-preset-es2015": "^6.13.2", | ||
"babel-register": "^6.11.6", | ||
"chai": "^3.5.0", | ||
"mocha": "^3.0.2", | ||
"rimraf": "^2.5.4" | ||
"@babel/cli": "^7.0.0-beta.53", | ||
"@babel/core": "^7.0.0-beta.53", | ||
"@babel/preset-env": "^7.0.0-beta.53", | ||
"@babel/register": "^7.0.0-beta.53", | ||
"babel-eslint": "^8.2.5", | ||
"babel-plugin-istanbul": "^4.1.6", | ||
"chai": "^4.1.2", | ||
"chokidar-cli": "^1.2.0", | ||
"cross-env": "^5.2.0", | ||
"eslint": "^5.1.0", | ||
"mocha": "^5.2.0", | ||
"nyc": "^12.0.2", | ||
"rimraf": "^2.6.2" | ||
} | ||
} |
import {expect} from 'chai' | ||
import Scanner from '../lib/Scanner' | ||
import Scanner from '../src' | ||
describe("Scanner", function () { | ||
var scanner = null; | ||
var dialectedScanner = null; | ||
var scanner = null | ||
var dialectedScanner = null | ||
@@ -14,3 +14,3 @@ beforeEach(function () { | ||
{"space": /\s+/} | ||
]); | ||
]) | ||
@@ -26,23 +26,32 @@ dialectedScanner = new Scanner({ | ||
] | ||
}); | ||
}); | ||
}) | ||
}) | ||
describe(`constructor()`, function () { | ||
it(`should throw if given duplicate type descriptor names`, function () { | ||
expect(() => new Scanner([ | ||
{"dot": /\./}, | ||
{"dot": /\./}, | ||
])).to.throw(/duplicate/) | ||
}) | ||
}) | ||
describe("#setSource", function () { | ||
it("should reset the dialect stack", function () { | ||
dialectedScanner.pushDialect("foo"); | ||
dialectedScanner.pushDialect("bar"); | ||
dialectedScanner.setSource("hi"); | ||
expect(dialectedScanner.dialectStack()).to.deep.equal([]); | ||
dialectedScanner.pushDialect("foo") | ||
dialectedScanner.pushDialect("bar") | ||
dialectedScanner.setSource("hi") | ||
expect(dialectedScanner.dialectStack()).to.deep.equal([]) | ||
}) | ||
it("should set the current dialect to null if scanner has more than one dialect", function () { | ||
dialectedScanner.pushDialect("foo"); | ||
dialectedScanner.pushDialect("bar"); | ||
dialectedScanner.setSource("hi"); | ||
expect(dialectedScanner.currentDialect()).to.be.null; | ||
dialectedScanner.pushDialect("foo") | ||
dialectedScanner.pushDialect("bar") | ||
dialectedScanner.setSource("hi") | ||
expect(dialectedScanner.currentDialect()).to.be.null | ||
}) | ||
it("should not set the current dialect to null if the scanner has only one dialect", function () { | ||
scanner.setSource("hi"); | ||
expect(scanner.currentDialect()).not.to.be.null; | ||
scanner.setSource("hi") | ||
expect(scanner.currentDialect()).not.to.be.null | ||
}) | ||
@@ -53,90 +62,94 @@ }) | ||
it("should be able to produce tokens", function () { | ||
scanner.setSource("net.desert"); | ||
scanner.setSource("net.desert") | ||
var netTok = scanner.nextToken(); | ||
var dotTok = scanner.nextToken(); | ||
var desTok = scanner.nextToken(); | ||
var eofTok = scanner.nextToken(); | ||
var netTok = scanner.nextToken() | ||
var dotTok = scanner.nextToken() | ||
var desTok = scanner.nextToken() | ||
var eofTok = scanner.nextToken() | ||
expect(netTok.type).to.equal("ident"); | ||
expect(netTok.value).to.equal("net"); | ||
expect(dotTok.type).to.equal("dot"); | ||
expect(desTok.type).to.equal("ident"); | ||
expect(desTok.value).to.equal("desert"); | ||
expect(eofTok).to.equal(null); | ||
}); | ||
expect(netTok.type).to.equal("ident") | ||
expect(netTok.value).to.equal("net") | ||
expect(dotTok.type).to.equal("dot") | ||
expect(desTok.type).to.equal("ident") | ||
expect(desTok.value).to.equal("desert") | ||
expect(eofTok).to.equal(null) | ||
}) | ||
it("should produce tokens with correct column and line numbers", function () { | ||
scanner.setSource("one two\n three\n\nfour"); | ||
scanner.setSource("one two\n three\n\nfour") | ||
var tok; | ||
var tokens = []; | ||
while (tok = scanner.nextToken()) { | ||
if (tok.type === "space") continue; | ||
tokens.push(tok); | ||
var tok | ||
var tokens = [] | ||
while ((tok = scanner.nextToken())) { | ||
if (tok.type === "space") continue | ||
tokens.push(tok) | ||
} | ||
expect(tokens[0].line).to.equal(1); | ||
expect(tokens[0].column).to.equal(1); | ||
expect(tokens[1].line).to.equal(1); | ||
expect(tokens[1].column).to.equal(5); | ||
expect(tokens[2].line).to.equal(2); | ||
expect(tokens[2].column).to.equal(2); | ||
expect(tokens[3].line).to.equal(4); | ||
expect(tokens[3].column).to.equal(1); | ||
}); | ||
expect(tokens[0].line).to.equal(1) | ||
expect(tokens[0].column).to.equal(1) | ||
expect(tokens[1].line).to.equal(1) | ||
expect(tokens[1].column).to.equal(5) | ||
expect(tokens[2].line).to.equal(2) | ||
expect(tokens[2].column).to.equal(2) | ||
expect(tokens[3].line).to.equal(4) | ||
expect(tokens[3].column).to.equal(1) | ||
}) | ||
it("should throw errors with relevant line and column numbers", function () { | ||
scanner.setSource("one\r\ntwo\r\nfail:here\r\nneversee"); | ||
scanner.setSource("one\r\ntwo\r\nfail:here\r\nneversee") | ||
var tok; | ||
var error = null; | ||
var tokens = []; | ||
var tok | ||
var error = null | ||
var tokens = [] | ||
try { | ||
while (tok = scanner.nextToken()) { | ||
if (tok.type === "space") continue; | ||
tokens.push(tok); | ||
while ((tok = scanner.nextToken())) { | ||
if (tok.type === "space") continue | ||
tokens.push(tok) | ||
} | ||
} | ||
catch (e) { | ||
error = e; | ||
error = e | ||
} | ||
expect(tokens.length).to.equal(3); | ||
expect(error).not.to.be.null; | ||
expect(error.name).to.equal("ScannerError"); | ||
expect(error.index).to.equal(14); | ||
expect(error.line).to.equal(3); | ||
expect(error.column).to.equal(5); | ||
}); | ||
expect(tokens.length).to.equal(3) | ||
expect(error).not.to.be.null | ||
expect(error.name).to.equal("ScannerError") | ||
expect(error.index).to.equal(14) | ||
expect(error.line).to.equal(3) | ||
expect(error.column).to.equal(5) | ||
}) | ||
it("should only process expected tokens", function () { | ||
scanner.setSource("word. word"); | ||
scanner.setSource("word. word") | ||
var tok = scanner.nextToken(["dot", "ident"]); | ||
expect(tok.type).to.equal("ident"); | ||
var tok = scanner.nextToken(["dot", "ident"]) | ||
expect(tok.type).to.equal("ident") | ||
tok = scanner.nextToken(["dot", "ident"]); | ||
expect(tok.type).to.equal("dot"); | ||
tok = scanner.nextToken(["dot", "ident"]) | ||
expect(tok.type).to.equal("dot") | ||
var error = null; | ||
var error = null | ||
try { | ||
tok = scanner.nextToken(["dot", "ident"]); | ||
tok = scanner.nextToken(["dot", "ident"]) | ||
} | ||
catch (e) { | ||
error = e; | ||
error = e | ||
} | ||
expect(error).not.to.be.null; | ||
}); | ||
expect(error).not.to.be.null | ||
}) | ||
it("should throw an error when there is no current dialect", function () { | ||
expect(function () { dialectedScanner.nextToken() }).to.throw(); | ||
expect(function () { dialectedScanner.nextToken() }).to.throw() | ||
}) | ||
it("should not crash when a LINE SEPARATOR character is in the input string", function () { | ||
scanner = new Scanner([ {"tagstart": /</}, {"text": /[^<]+/} ]); | ||
scanner.setSource("hello\n world \u2028 <br>\n foo\n"); | ||
var generateTokens = function () { while (scanner.nextToken()) { /* noop */ } }; | ||
expect(generateTokens).not.to.throw(); | ||
scanner = new Scanner([ {"tagstart": /</}, {"text": /[^<]+/} ]) | ||
scanner.setSource("hello\n world \u2028 <br>\n foo\n") | ||
var generateTokens = function () { while (scanner.nextToken()) { /* noop */ } } | ||
expect(generateTokens).not.to.throw() | ||
}) | ||
it(`should throw an error if .setSource() has not been called`, function () { | ||
expect(() => scanner.nextToken()).to.throw(/source string was not set/i) | ||
}) | ||
}) | ||
@@ -146,3 +159,3 @@ | ||
it("should return 'main' when the original dialect is the active one", function () { | ||
expect(scanner.currentDialect()).to.equal("main"); | ||
expect(scanner.currentDialect()).to.equal("main") | ||
}) | ||
@@ -153,25 +166,36 @@ }) | ||
it("should return an array of available dialect names", function () { | ||
expect(scanner.dialects()).to.deep.equal(["main"]); | ||
expect(scanner.dialects()).to.deep.equal(["main"]) | ||
}) | ||
}) | ||
describe("#addDialect", function () { | ||
it(`should throw when passed dialect has already been added`, function () { | ||
expect(() => dialectedScanner.addDialect('foo', [{'dot':/\./}])) | ||
.to.throw(/already exists/i) | ||
}) | ||
}) | ||
describe("#setDialect", function () { | ||
it("should cause the scanner to use the specified dialect when fetching the next token", function () { | ||
var str = "42hello"; | ||
dialectedScanner.setSource(str); | ||
var str = "42hello" | ||
dialectedScanner.setSource(str) | ||
dialectedScanner.setDialect("foo"); | ||
var tok1 = dialectedScanner.nextToken(); | ||
expect(tok1.type).to.equal("num"); | ||
dialectedScanner.setDialect("foo") | ||
var tok1 = dialectedScanner.nextToken() | ||
expect(tok1.type).to.equal("num") | ||
dialectedScanner.setDialect("bar"); | ||
var tok2 = dialectedScanner.nextToken(); | ||
expect(tok2.type).to.equal("word"); | ||
dialectedScanner.setDialect("bar") | ||
var tok2 = dialectedScanner.nextToken() | ||
expect(tok2.type).to.equal("word") | ||
}) | ||
}); | ||
it(`should throw if passed dialect has not been added`, function () { | ||
expect(() => scanner.setDialect('nonexist')).to.throw(/No such dialect/i) | ||
}) | ||
}) | ||
describe("#pushDialect", function () { | ||
it("should set the current dialect", function () { | ||
dialectedScanner.pushDialect("bar"); | ||
expect(dialectedScanner.currentDialect()).to.equal("bar"); | ||
dialectedScanner.pushDialect("bar") | ||
expect(dialectedScanner.currentDialect()).to.equal("bar") | ||
}) | ||
@@ -182,8 +206,8 @@ }) | ||
it("should set the current dialect to the dialect on top of the stack", function () { | ||
dialectedScanner.pushDialect("foo"); | ||
dialectedScanner.pushDialect("bar"); | ||
dialectedScanner.popDialect(); | ||
expect(dialectedScanner.currentDialect()).to.equal("foo"); | ||
dialectedScanner.pushDialect("foo") | ||
dialectedScanner.pushDialect("bar") | ||
dialectedScanner.popDialect() | ||
expect(dialectedScanner.currentDialect()).to.equal("foo") | ||
}) | ||
}) | ||
}); | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Unidentified License
License(Experimental) Something that seems like a license was found, but its contents could not be matched with a known license.
Found 1 instance in 1 package
102686
56.25%25
47.06%0
-100%0
-100%100
25%1102
10.64%13
116.67%1
Infinity%