Inherit tokenizer from core

This commit is contained in:
Liam Newman 2018-08-03 21:34:49 -07:00
parent bb5d1dd571
commit 2e9a299851
2 changed files with 144 additions and 51 deletions

111
js/src/core/tokenizer.js Normal file
View File

@ -0,0 +1,111 @@
/*jshint curly:true, eqeqeq:true, laxbreak:true, noempty:false */
/*
The MIT License (MIT)
Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
var InputScanner = require('../core/inputscanner').InputScanner;
var Token = require('../core/token').Token;
var TOKEN = {
RAW: 'TK_RAW',
EOF: 'TK_EOF'
};
function Tokenizer(input_string, opts) { // jshint unused:false
this._input = null;
this._tokens = null;
this.tokenize = function() {
this._input = new InputScanner(input_string);
this._tokens = []; //new TokenStream();
this.reset();
var current, last;
var open_token = null;
var open_stack = [];
var comments = [];
while (!(last && last.type === TOKEN.EOF)) {
current = this.get_next_token();
while (this.is_comment(current)) {
comments.push(current);
current = this.get_next_token();
}
if (comments.length) {
current.comments_before = comments;
comments = [];
}
if (this.is_opening(current)) {
current.parent = last;
open_stack.push(open_token);
open_token = current;
} else if (open_token && this.is_closing(current, open_token)) {
current.parent = open_token.parent;
current.opened = open_token;
open_token = open_stack.pop();
}
this._tokens.push(current);
last = current;
}
return this._tokens;
};
this.reset = function() {
};
this.get_next_token = function() {
var resulting_string = this._input.readWhile(/.+/g);
if (resulting_string) {
return new Token(TOKEN.RAW, resulting_string, 0, '');
} else {
return new Token(TOKEN.EOF, '', 0, '');
}
};
this.is_comment = function(current_token) { // jshint unused:false
return false;
};
this.is_opening = function(current_token) { // jshint unused:false
return false;
};
this.is_closing = function(current_token, open_token) { // jshint unused:false
return false;
};
}
module.exports.Tokenizer = Tokenizer;
module.exports.TOKEN = TOKEN;

View File

@ -28,13 +28,15 @@
var InputScanner = require('../core/inputscanner').InputScanner;
var Token = require('../core/token').Token;
var BaseTokenizer = require('../core/tokenizer').Tokenizer;
var BASETOKEN = require('../core/tokenizer').TOKEN;
var acorn = require('../core/acorn');
function in_array(what, arr) {
return arr.indexOf(what) !== -1;
}
var TOKEN = {
START_EXPR: 'TK_START_EXPR',
END_EXPR: 'TK_END_EXPR',
@ -51,16 +53,16 @@ var TOKEN = {
COMMENT: 'TK_COMMENT',
DOT: 'TK_DOT',
UNKNOWN: 'TK_UNKNOWN',
EOF: 'TK_EOF'
EOF: BASETOKEN.EOF
};
function Tokenizer(input_string, opts) {
BaseTokenizer.call(this, input_string, opts);
var whitespacePattern = /[\n\r\u2028\u2029\t ]+/g;
var newlinePattern = /([\t ]*)(\r\n|[\n\r\u2028\u2029])?/g;
var number_pattern = /0[xX][0123456789abcdefABCDEF]*|0[oO][01234567]*|0[bB][01]*|\d+n|(?:\.\d+|\d+\.?\d*)(?:[eE][+-]?\d+)?/g;
var digit = /[0-9]/;
this.positionable_operators = '!= !== % & && * ** + - / : < << <= == === > >= >> >>> ? ^ | ||'.split(' ');
@ -84,57 +86,37 @@ function Tokenizer(input_string, opts) {
var template_pattern = /((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)/g;
var n_newlines, whitespace_before_token, in_html_comment, tokens;
var input;
var n_newlines, whitespace_before_token, in_html_comment, tokens, input;
this.tokenize = function() {
input = new InputScanner(input_string);
in_html_comment = false;
tokens = [];
var next, last;
var token_values;
var open = null;
var open_stack = [];
var comments = [];
while (!(last && last.type === TOKEN.EOF)) {
token_values = tokenize_next();
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token);
while (next.type === TOKEN.COMMENT || next.type === TOKEN.BLOCK_COMMENT || next.type === TOKEN.UNKNOWN) {
if (next.type === TOKEN.BLOCK_COMMENT) {
next.directives = token_values[2];
}
comments.push(next);
token_values = tokenize_next();
next = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token);
}
if (comments.length) {
next.comments_before = comments;
comments = [];
}
if (next.type === TOKEN.START_BLOCK || next.type === TOKEN.START_EXPR) {
next.parent = last;
open_stack.push(open);
open = next;
} else if ((next.type === TOKEN.END_BLOCK || next.type === TOKEN.END_EXPR) &&
(open && (
(next.text === ']' && open.text === '[') ||
(next.text === ')' && open.text === '(') ||
(next.text === '}' && open.text === '{')))) {
next.parent = open.parent;
next.opened = open;
open = open_stack.pop();
}
tokens.push(next);
last = next;
this.get_next_token = function() {
var token_values = tokenize_next();
var token = new Token(token_values[1], token_values[0], n_newlines, whitespace_before_token);
if (token.type === TOKEN.BLOCK_COMMENT) {
token.directives = token_values[2];
}
return token;
};
return tokens;
this.is_comment = function(current_token) {
return current_token.type === TOKEN.COMMENT || current_token.type === TOKEN.BLOCK_COMMENT || current_token.type === TOKEN.UNKNOWN;
};
this.is_opening = function(current_token) {
return current_token.type === TOKEN.START_BLOCK || current_token.type === TOKEN.START_EXPR;
};
this.is_closing = function(current_token, open_token) {
return (current_token.type === TOKEN.END_BLOCK || current_token.type === TOKEN.END_EXPR) &&
(open_token && (
(current_token.text === ']' && open_token.text === '[') ||
(current_token.text === ')' && open_token.text === '(') ||
(current_token.text === '}' && open_token.text === '{')));
};
this.reset = function() {
in_html_comment = false;
input = this._input;
tokens = this._tokens;
};
function get_directives(text) {