~psmay/+junk/prasky.dev

« back to all changes in this revision

Viewing changes to praskyParser.jison

  • Committer: Peter S. May
  • Date: 2012-10-25 02:04:41 UTC
  • Revision ID: peter_s._may_httppsmay.com-20121025020441-qqz9r3idkhk2jsih
Initial. Doesn't parse () correctly yet.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
%lex
 
3
 
 
4
%s codes trans
 
5
 
 
6
%%
 
7
 
 
8
<INITIAL>[\s\S]+?(?="#(")               { this.pushState('trans'); return 'TEXT'; }
 
9
<INITIAL>[\s\S]+                                { return 'TEXT'; }
 
10
 
 
11
<trans>"#"(?="(")                               { return 'TOPIC_SIGILS'; }
 
12
<trans>"("                                              { this.popState(); this.pushState('codes'); return 'DOWN'; }
 
13
 
 
14
<codes>[\$@#]+(?=[\r\n\s)])             { return 'TOPIC_SIGILS_EMPTY'; }
 
15
<codes>[\$@#]+                                  { return 'TOPIC_SIGILS'; }
 
16
<codes>"("                                              { this.pushState('codes'); return 'DOWN'; }
 
17
 
 
18
<codes>"\""{3}[\s\S]*?"\""{3,}  { return 'QUOTES3'; }
 
19
<codes>"'"{3}[\s\S]*?"'"{3,}    { return 'QUOTES3'; }
 
20
 
 
21
<codes>"\""(?:[^"\x5c]|\\[\s\S])*?"\""          { return 'QUOTES1'; }
 
22
<codes>"'"(?:[^'\x5c]|\\[\s\S])*?"'"            { return 'QUOTES1'; }
 
23
 
 
24
<codes>[^\r\n\s)]+                              { return 'QUOTES0'; }
 
25
 
 
26
<codes>")"                                              { this.popState(); return 'UP'; }
 
27
<codes>[\r\n\s]+                                /* skip */
 
28
 
 
29
 
 
30
/lex
 
31
 
 
32
%start top
 
33
 
 
34
%%
 
35
 
 
36
top
 
37
        : { $$ = []; return $$; }
 
38
        | items { $$ = $1; return $$; }
 
39
        ;
 
40
 
 
41
items
 
42
        : item { $$ = [$1]; }
 
43
        | items item { $$ = $1.concat([$2]); }
 
44
        ;
 
45
 
 
46
item : literal | string | node ;
 
47
 
 
48
literal
 
49
        : TEXT { $$ = MARKUP.literalText($1); }
 
50
        ;
 
51
 
 
52
string
 
53
        : string_nosigil { $$ = $1; }
 
54
        | TOPIC_SIGILS string_nosigil { $$ = MARKUP.withSigils($1,$2); }
 
55
        ;
 
56
 
 
57
string_nosigil
 
58
        : QUOTES3 { $$ = MARKUP.quote3($1.slice(3,-3), $1.slice(0,3)); }
 
59
        | QUOTES1 { $$ = MARKUP.quote1($1.slice(1,-1), $1.slice(0,1)); }
 
60
        | QUOTES0 { $$ = MARKUP.quote0($1); }
 
61
        ;
 
62
 
 
63
node
 
64
        : node_nosigil
 
65
        | TOPIC_SIGILS node_nosigil { $$ = MARKUP.withSigils($1,$2); }
 
66
        | TOPIC_SIGILS_EMPTY { $$ = MARKUP.withSigils($1, MARKUP.quoteImpliedEmpty()); }
 
67
        ;
 
68
 
 
69
node_nosigil
 
70
        : DOWN items UP { $$ = MARKUP.node($2) }
 
71
        ;
 
72
 
 
73
 
 
74
%%
 
75
 
 
76
var MARKUP = {
 
77
        each : function(map, fn) {
 
78
                for(var k in map) {
 
79
                        if(Object.prototype.hasOwnProperty.call(map, k)) {
 
80
                                if(fn(map[k], k) === false) break;
 
81
                        }
 
82
                }
 
83
        },
 
84
        tag : function(name, data, more) {
 
85
                return MARKUP.tagObject(name, data, more);
 
86
        },
 
87
        tagObject : function(name, data, more) {
 
88
                var rv = {};
 
89
                MARKUP.each(more, function(v,k) {
 
90
                        rv[k] = v;
 
91
                });
 
92
                if(String(name) === name)
 
93
                        rv[" NAME"] = name;
 
94
                if(!(data === null || data === void(0)))
 
95
                        rv[" DATA"] = data;
 
96
                return rv;
 
97
        },
 
98
        withOneSigil : function(sigil, item) {
 
99
                return MARKUP.tag("sigils", item, { sigil : sigil });
 
100
        },
 
101
        withSigils : function(sigils, item) {
 
102
                return (sigils.length <= 1) ?
 
103
                        MARKUP.withOneSigil(sigils, item) :
 
104
                        MARKUP.withOneSigil(sigils.slice(0,1), MARKUP.withSigils(sigils.slice(1), item)); 
 
105
        },
 
106
        quote3 : function(rawText, quote) {
 
107
                return MARKUP.tag("string", rawText, { quote : quote });
 
108
        },
 
109
        quote1 : function(rawText, quote) {
 
110
                var text = rawText.replace(/\\(?:x([0-9A-Fa-f]{2})|u([0-9A-Fa-f]{4})|([\S\s]))/g, function(match, xc, uc, sc, offset, string) {
 
111
                        if(String(sc) === sc) {
 
112
                                return sc;
 
113
                        }
 
114
                        else {
 
115
                                var h = parseInt( (String(xc) === xc) ? xc : uc, 16 );
 
116
                                return String.fromCharCode(h);
 
117
                        }
 
118
                });
 
119
                return MARKUP.tag("string", text, { quote : quote, rawText : rawText });
 
120
        },
 
121
        quote0 : function(rawText) {
 
122
                return MARKUP.tag("string", rawText);
 
123
        },
 
124
        quoteImpliedEmpty : function() {
 
125
                return MARKUP.tag("string");
 
126
        },
 
127
        literalText : function(text) {
 
128
                return MARKUP.tag("literal", text);
 
129
        },
 
130
        node : function(list) {
 
131
                return MARKUP.tag("node", list);
 
132
        }
 
133
};