UNPKG

2.1 kBJavaScriptView Raw
1var block = {
2 word_break: "[^\\w]",
3 text: "[\\w]"
4};
5
6var v4_regex_str = "[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}";
7
8/**
9 * Normal Block Grammar
10 */
11
12block.normal = merge({}, block);
13
14/**
15 * Block Lexer
16 */
17
18function Lexer(options) {
19 options = options || {};
20 this.tokens = [];
21 this.tokens.links = {};
22 this.options = options;
23 this.options.loggingEnabled = false;
24 this.rules = block.normal;
25}
26
27/**
28 * Expose Block Rules
29 */
30Lexer.rules = block;
31
32/**
33 * Static Lex Method
34 */
35Lexer.lex = function(src, options) {
36 var lexer = new Lexer(options);
37 return lexer.lex(src);
38};
39
40/**
41 * Preprocessing
42 */
43Lexer.prototype.lex = function(src) {
44 return this.token(src, true);
45};
46
47/**
48 * Lexing
49 */
50Lexer.prototype.token = function(src, top) {
51 var tokens_match_str = "(" + this.rules.word_break + ")|(" + this.rules.text + ")";
52 var regex = new RegExp(tokens_match_str, "ig");
53
54 var result, tokens = [];
55 var iteration = 0;
56 while ((result = regex.exec(src)) !== null) {
57 if (result[1]) {
58 this.log({
59 type: 'break',
60 match: result[0]
61 });
62 this.tokens.push({
63 type: 'break',
64 raw: result[0],
65 index : result.index
66 });
67 continue;
68 }
69 if (result[2]) {
70 this.log({
71 type: 'text',
72 match: result[0]
73 });
74 this.tokens.push({
75 type: 'text',
76 raw: result[0],
77 index : result.index
78 });
79 continue;
80 }
81 }
82 return this.tokens;
83};
84
85Lexer.prototype.log = function(msg) {
86 if (this.options.loggingEnabled) {
87 console.log(msg);
88 }
89};
90
91
92// Helpers
93
94function merge(obj) {
95 var i = 1,
96 target, key;
97
98 for (; i < arguments.length; i++) {
99 target = arguments[i];
100 for (key in target) {
101 if (Object.prototype.hasOwnProperty.call(target, key)) {
102 obj[key] = target[key];
103 }
104 }
105 }
106
107 return obj;
108}
109
110// export the class
111if (typeof exports === 'object') {
112 module.exports = Lexer;
113} else if (typeof define === 'function' && define.amd) {
114 define(function() {
115 return Lexer;
116 });
117} else {
118 this.Lexer = Lexer;
119}
\No newline at end of file