UNPKG

3.11 kBJavaScriptView Raw
1// The order of these is important, as it is the order in which
2// they are run against the input string.
3// They are separated out here to allow for better minification
4// with the least amount of effort from me. :)
5
6// Any function instead of regex is called with the lexer as the
7// context.
8
9// NOTE: this is an array, not an object literal! The () around
10// the regexps are for the sake of the syntax highlighter in my
11// editor... sublimetext2
12
13var TESTS = [
14
15 // A real email address is considerably more complex, and unfortunately
16 // this complexity makes it impossible to differentiate between an address
17 // and an AT expression.
18 //
19 // Instead, this regex assumes the only valid characters for the user portion
20 // of the address are alphanumeric, period, and %. This means that a complex email like
21 // who-something@example.com will be interpreted as an email, but incompletely. `who-`
22 // will be content, while `something@example.com` will be the email address.
23 //
24 // However, this is "Good Enough"© :).
25 'EMAIL', (/^([a-zA-Z0-9.%]+@[a-zA-Z0-9.\-]+\.(?:[a-z]{2}|co\.uk|com|edu|net|org))\b/)
26
27 , 'AT_STAR_OPEN', (/^(@\*)/)
28 , 'AT_STAR_CLOSE', (/^(\*@)/)
29
30
31 , 'AT_COLON', (/^(@\:)/)
32 , 'AT', (/^(@)/)
33
34
35 , 'PAREN_OPEN', (/^(\()/)
36 , 'PAREN_CLOSE', (/^(\))/)
37
38
39 , 'HARD_PAREN_OPEN', (/^(\[)/)
40 , 'HARD_PAREN_CLOSE', (/^(\])/)
41
42
43 , 'BRACE_OPEN', (/^(\{)/)
44 , 'BRACE_CLOSE', (/^(\})/)
45
46
47 , 'HTML_TAG_VOID_CLOSE', (/^(\/>)/)
48 , 'HTML_TAG_CLOSE', (/^(<\/)/)
49 , 'HTML_COMMENT_OPEN', (/^(<!--+)/)
50 , 'HTML_COMMENT_CLOSE', (/^(--+>)/)
51 , 'LT_SIGN', (/^(<)/)
52 , 'GT_SIGN', (/^(>)/)
53
54 , 'ASSIGNMENT_OPERATOR', (/^(\|=|\^=|&=|>>>=|>>=|<<=|-=|\+=|%=|\/=|\*=)\b/) // Also =
55 , 'EQUALITY_OPERATOR', (/^(===|==|!==|!=)\b/)
56 , 'BITWISE_SHIFT_OPERATOR', (/^(<<|>>>|>>)/)
57 , 'UNARY_OPERATOR', (/^(delete\b|typeof\b|void|\+\+|--|\+|-|~|!)/)
58 , 'RELATIONAL_OPERATOR', (/^(<=|>=|instanceof|in)\b/) // Also <, >
59 , 'BINARY_LOGICAL_OPERATOR', (/^(&&|\|\|)\b/)
60 , 'BINARY_BITWISE_OPERATOR', (/^(&|\^|\|)\b/)
61 , 'NEW_OPERATOR', (/^(new)\b/)
62 , 'COMMA_OPERATOR', (/^(,)/)
63
64 , 'EQUAL_SIGN', (/^(=)/)
65 , 'COLON', (/^(:)/)
66 , 'PERIOD', (/^(\.)/)
67 , 'NEWLINE', function(){
68 var token = this.scan(/^(\n)/, exports.NEWLINE);
69 if(token){
70 this.lineno++;
71 this.charno = 0;
72 }
73 return token;
74 }
75 , 'WHITESPACE', (/^([^\S\n]+)/) // http://stackoverflow.com/a/3469155
76 , 'FUNCTION', (/^(function)(?![\d\w])/)
77 , 'BLOCK_KEYWORD', (/^(catch|do|else if|else|finally|for|function|goto|if|switch|try|while|with)(?![\d\w])/)
78 , 'KEYWORD', (/^(break|case|continue|instanceof|return|var)(?![\d\w])/)
79 , 'IDENTIFIER', (/^([_$a-zA-Z\xA0-\uFFFF][_$a-zA-Z0-9\xA0-\uFFFF]*)/)
80
81 , 'DOUBLE_FORWARD_SLASH', (/^(\/\/)/)
82
83 , 'FORWARD_SLASH', (/^(\/)/)
84
85 , 'BACKSLASH', (/^(\\)/)
86 , 'EXCLAMATION_POINT', (/^(!)/)
87 , 'DOUBLE_QUOTE', (/^(\")/)
88 , 'SINGLE_QUOTE', (/^(\')/)
89
90 , 'NUMERAL', (/^([0-9])/)
91 , 'CONTENT', (/^([^\s])/)
92
93];
94
95exports.tests = TESTS;
96
97// Export all the tokens as constants.
98for(var i = 0; i < TESTS.length; i += 2) {
99 exports[TESTS[i]] = TESTS[i];
100}
\No newline at end of file