Skip to content

Commit 0356720

Browse files
committed
fix lexer & cover with tests
1 parent 2c8e479 commit 0356720

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

src/lexer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ var lexerWhiteSpace = [' ', '\t', '\r', '\n'];
2828
*/
2929
Lexer.prototype.read = function (input) {
3030
this._input = input;
31-
this.line = 0;
31+
this.line = 1;
3232
this.offset = 0;
3333
this.text = '';
3434
this.token = null;
35+
this.backup = null;
3536
};
3637

3738
/**
@@ -89,6 +90,7 @@ Lexer.prototype.unlex = function (state) {
8990
this.token = state.token;
9091
this.line = state.line;
9192
}
93+
this.backup = null;
9294
return this.token;
9395
};
9496

test/lexer.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*!
2+
* Copyright (C) 2017 Glayzzle (BSD3 License)
3+
* @authors https://github.com/glayzzle/docblock-parser/graphs/contributors
4+
* @url http://glayzzle.com/docblock-parser
5+
*/
6+
7+
// eslint-disable-next-line no-unused-vars
8+
var should = require('should');
9+
var tokens = require('../src/token');
10+
var Lexer = require('../src/lexer');
11+
12+
describe('Test lexer', function () {
13+
var reader = new Lexer(tokens);
14+
describe('Test windows lines', function () {
15+
reader.read([
16+
'hello',
17+
'world'
18+
].join('\r\n'));
19+
20+
reader.lex().should.be.exactly(tokens.T_STRING);
21+
reader.text.should.be.exactly('hello');
22+
reader.line.should.be.exactly(1);
23+
24+
reader.next().should.be.exactly(tokens.T_WHITESPACE);
25+
reader.text.should.be.exactly('\r\n');
26+
reader.line.should.be.exactly(2);
27+
28+
reader.unput();
29+
reader.line.should.be.exactly(1);
30+
31+
reader.lex().should.be.exactly(tokens.T_STRING);
32+
reader.text.should.be.exactly('world');
33+
reader.line.should.be.exactly(2);
34+
});
35+
describe('Test mac lines', function () {
36+
reader.read([
37+
'hello',
38+
'world'
39+
].join('\r'));
40+
41+
reader.lex().should.be.exactly(tokens.T_STRING);
42+
reader.text.should.be.exactly('hello');
43+
reader.line.should.be.exactly(1);
44+
45+
reader.lex().should.be.exactly(tokens.T_STRING);
46+
reader.text.should.be.exactly('world');
47+
reader.line.should.be.exactly(2);
48+
});
49+
describe('Test unlex', function () {
50+
reader.read([
51+
'hello',
52+
'world'
53+
].join('\n'));
54+
55+
// read
56+
var start = reader.state();
57+
reader.unlex();
58+
reader.offset.should.be.exactly(0);
59+
60+
// next
61+
reader.lex().should.be.exactly(tokens.T_STRING);
62+
reader.lex().should.be.exactly(tokens.T_STRING);
63+
reader.line.should.be.exactly(2);
64+
65+
// go back
66+
reader.unlex(start);
67+
reader.lex().should.be.exactly(tokens.T_STRING);
68+
reader.line.should.be.exactly(1);
69+
reader.text.should.be.exactly('hello');
70+
71+
// next
72+
reader.lex().should.be.exactly(tokens.T_STRING);
73+
reader.line.should.be.exactly(2);
74+
75+
// back
76+
reader.unlex();
77+
reader.line.should.be.exactly(1);
78+
reader.text.should.be.exactly('hello');
79+
80+
});
81+
describe('Test float', function () {
82+
reader.read('1.2.3');
83+
reader.lex().should.be.exactly(tokens.T_NUM);
84+
reader.text.should.be.exactly('1.2');
85+
reader.lex().should.be.exactly(tokens.T_NUM);
86+
reader.text.should.be.exactly('.3');
87+
});
88+
describe('Test assign', function () {
89+
reader.read('foo => a, bar: b');
90+
reader.lex().should.be.exactly(tokens.T_STRING);
91+
reader.lex().should.be.exactly('=>');
92+
reader.lex().should.be.exactly(tokens.T_STRING);
93+
reader.lex().should.be.exactly(',');
94+
reader.lex().should.be.exactly(tokens.T_STRING);
95+
reader.lex().should.be.exactly('=>');
96+
reader.lex().should.be.exactly(tokens.T_STRING);
97+
});
98+
describe('Test text', function () {
99+
reader.read('\'aze\\\'rty\'');
100+
reader.lex().should.be.exactly(tokens.T_TEXT);
101+
reader.text.should.be.exactly('\'aze\\\'rty\'');
102+
});
103+
});

0 commit comments

Comments
 (0)