33 * @authors https://github.com/glayzzle/docblock-parser/graphs/contributors
44 * @url http://glayzzle.com/docblock-parser
55 */
6-
7- "use strict" ;
6+ 'use strict' ;
87
98/**
109 * @constructor Lexer
1110 * @property {String } text Current parsed text (attached to current token)
1211 * @property {Number } offset Current offset
1312 * @property {String|Number } token Current parsed token
1413 */
15- var Lexer = function ( tokens ) {
14+ var Lexer = function ( tokens ) {
1615 this . _t = tokens ;
1716} ;
1817
19-
2018// breaking symbols
2119var lexerSymbols = [
2220 ',' , '=' , ':' , '(' , ')' , '[' , ']' , '{' , '}' , '@'
@@ -28,32 +26,31 @@ var lexerWhiteSpace = [' ', '\t', '\r', '\n'];
2826/**
2927 * Initialize the lexer with specified text
3028 */
31- Lexer . prototype . read = function ( input ) {
29+ Lexer . prototype . read = function ( input ) {
3230 this . _input = input ;
3331 this . offset = 0 ;
34- this . text = "" ;
32+ this . text = '' ;
3533 this . token = null ;
3634} ;
3735
3836/**
3937 * Consumes a char
4038 * @return {String }
4139 */
42- Lexer . prototype . input = function ( ) {
40+ Lexer . prototype . input = function ( ) {
4341 if ( this . offset < this . _input . length ) {
4442 this . ch = this . _input [ this . offset ++ ] ;
4543 this . text += this . ch ;
4644 return this . ch ;
47- } else {
48- return null ;
4945 }
46+ return null ;
5047} ;
5148
5249/**
5350 * Revert back the current consumed char
5451 * @return {void }
5552 */
56- Lexer . prototype . unput = function ( ) {
53+ Lexer . prototype . unput = function ( ) {
5754 this . offset -- ;
5855 this . text = this . text . substring ( 0 , this . text . length - 1 ) ;
5956} ;
@@ -62,7 +59,7 @@ Lexer.prototype.unput = function() {
6259 * Revert back the current consumed token
6360 * @return {String|Number } the previous token
6461 */
65- Lexer . prototype . unlex = function ( ) {
62+ Lexer . prototype . unlex = function ( ) {
6663 this . offset = this . __offset ;
6764 this . text = this . __text ;
6865 this . token = this . __token ;
@@ -73,7 +70,7 @@ Lexer.prototype.unlex = function() {
7370 * Consumes the next token (ignores whitespaces)
7471 * @return {String|Number } the current token
7572 */
76- Lexer . prototype . lex = function ( ) {
73+ Lexer . prototype . lex = function ( ) {
7774 // backup
7875 this . __offset = this . offset ;
7976 this . __text = this . text ;
@@ -91,11 +88,12 @@ Lexer.prototype.lex = function() {
9188 * Eats a token (see lex for public usage) including whitespace
9289 * @return {String|Number } the current token
9390 */
94- Lexer . prototype . next = function ( ) {
95- this . text = "" ;
91+ Lexer . prototype . next = function ( ) {
92+ this . text = '' ;
9693 var ch = this . input ( ) ;
97- if ( ch === null ) return this . _t . T_EOF ;
98- if ( ch === '"' || ch === "'" ) {
94+ if ( ch === null ) {
95+ return this . _t . T_EOF ;
96+ } else if ( ch === '"' || ch === '\'' ) {
9997 var tKey = ch ;
10098 do {
10199 ch = this . input ( ) ;
@@ -105,9 +103,9 @@ Lexer.prototype.next = function() {
105103 } while ( ch !== tKey && this . offset < this . _input . length ) ;
106104 return this . _t . T_TEXT ;
107105 } else if ( lexerSymbols . indexOf ( ch ) > - 1 ) {
108- if ( ch === ':' )
106+ if ( ch === ':' ) {
109107 ch = '=>' ; // alias
110- if ( ch === '=' && this . _input [ this . offset ] === '>' ) {
108+ } else if ( ch === '=' && this . _input [ this . offset ] === '>' ) {
111109 ch += this . input ( ) ;
112110 }
113111 return ch ;
@@ -116,32 +114,35 @@ Lexer.prototype.next = function() {
116114 while ( lexerWhiteSpace . indexOf ( ch ) > - 1 ) {
117115 ch = this . input ( ) ;
118116 }
119- if ( ch !== null ) this . unput ( ) ;
117+ if ( ch !== null ) {
118+ this . unput ( ) ;
119+ }
120120 return this . _t . T_WHITESPACE ;
121- } else {
122- ch = ch . charCodeAt ( 0 ) ;
123- if ( ch > 47 && ch < 58 ) {
124- while ( ch > 47 && ch < 58 && ch !== null ) {
125- ch = this . input ( ) ;
126- if ( ch !== null )
127- ch = ch . charCodeAt ( 0 ) ;
121+ }
122+ ch = ch . charCodeAt ( 0 ) ;
123+ if ( ch > 47 && ch < 58 ) {
124+ while ( ch > 47 && ch < 58 && ch !== null ) {
125+ ch = this . input ( ) ;
126+ if ( ch !== null ) {
127+ ch = ch . charCodeAt ( 0 ) ;
128128 }
129- if ( ch !== null ) this . unput ( ) ;
130- return this . _t . T_NUM ;
131- } else {
132- do {
133- ch = this . input ( ) ;
134- if (
135- lexerSymbols . indexOf ( ch ) > - 1 ||
136- lexerWhiteSpace . indexOf ( ch ) > - 1
137- ) {
138- this . unput ( ) ;
139- break ;
140- }
141- } while ( this . offset < this . _input . length ) ;
142- return this . _t . T_STRING ;
143129 }
130+ if ( ch !== null ) {
131+ this . unput ( ) ;
132+ }
133+ return this . _t . T_NUM ;
144134 }
135+ do {
136+ ch = this . input ( ) ;
137+ if (
138+ lexerSymbols . indexOf ( ch ) > - 1 ||
139+ lexerWhiteSpace . indexOf ( ch ) > - 1
140+ ) {
141+ this . unput ( ) ;
142+ break ;
143+ }
144+ } while ( this . offset < this . _input . length ) ;
145+ return this . _t . T_STRING ;
145146} ;
146147
147148// exports
0 commit comments