1
1
( function ( $ ) {
2
2
3
+ var worker = new Worker ( '../assets/js/worker.js' ) ;
4
+
3
5
var parser ,
4
6
parser2 ;
5
7
@@ -13,8 +15,20 @@ print = function (){}
13
15
14
16
var printOut = function ( str ) { $ ( "#out" ) . html ( str ) ; } ;
15
17
18
+ function debounce ( timeout , fn ) {
19
+ var timer ;
20
+
21
+ return function ( ) {
22
+ clearTimeout ( timer ) ;
23
+
24
+ timer = setTimeout ( function ( ) {
25
+ fn ( ) ;
26
+ timer = null ;
27
+ } , timeout ) ;
28
+ } ;
29
+ }
30
+
16
31
$ ( document ) . ready ( function ( ) {
17
- $ ( "#process_btn" ) . click ( processGrammar ) ;
18
32
$ ( "#parse_btn" ) . click ( runParser ) ;
19
33
20
34
$ ( "#examples" ) . change ( function ( ev ) {
@@ -23,52 +37,107 @@ $(document).ready(function () {
23
37
$ . get ( "/jison/examples/" + file , function ( data ) {
24
38
$ ( "#grammar" ) . val ( data ) ;
25
39
$ ( document . body ) . removeClass ( "loading" ) ;
40
+ processGrammar ( ) ;
26
41
} ) ;
27
42
} ) ;
28
-
43
+
44
+ // recompile the grammar using a web worker,
45
+ // as the user types
46
+ var onChange = debounce ( 700 , processGrammar ) ;
47
+ $ ( '#grammar' ) . bind ( 'input propertychange' , onChange ) ;
48
+ processGrammar ( ) ;
29
49
} ) ;
30
50
31
51
function processGrammar ( ) {
32
- var type = "lalr" ;
33
-
34
- var grammar = $ ( "#grammar" ) . val ( ) ;
35
- try {
36
- var cfg = JSON . parse ( grammar ) ;
37
- } catch ( e ) {
52
+
53
+ function onError ( e ) {
54
+ console . log ( e ) ;
55
+ $ ( "#gen_out" ) . html ( "Oops. Make sure your grammar is " +
56
+ "in the correct format." + "\n" + e . stack )
57
+ . removeClass ( 'good' )
58
+ . removeClass ( 'warning' )
59
+ . addClass ( 'bad' ) ;
60
+ }
61
+
62
+ function onSuccess ( result ) {
63
+
38
64
try {
39
- var cfg = bnf . parse ( grammar ) ;
65
+ parser = Jison . Generator ( result . cfg , { type : result . type } ) ;
40
66
} catch ( e ) {
41
- $ ( "#gen_out" ) . html ( "Oops. Make sure your grammar is in the correct format.\n" + e ) . addClass ( 'bad' ) ;
42
- return ;
67
+ return onError ( e ) ;
68
+ }
69
+
70
+ $ ( "#out" ) . removeClass ( "good" ) . removeClass ( "bad" ) . html ( '' ) ;
71
+ $ ( "#gen_out" ) . removeClass ( "good" ) . removeClass ( "bad" ) . removeClass ( 'warning' ) ;
72
+ if ( ! parser . conflicts ) {
73
+ $ ( "#gen_out" ) . html ( 'Generated successfully!' ) . addClass ( 'good' ) ;
74
+ } else {
75
+ $ ( "#gen_out" ) . html ( 'Conflicts encountered:<br/>' ) . addClass ( 'bad' ) ;
43
76
}
44
- }
45
77
46
- Jison . print = function ( ) { } ;
47
- parser = Jison . Generator ( cfg , { type : type } ) ;
78
+ $ ( "#download_btn" ) . click ( function ( ) {
79
+ window . location . href = "data:application/javascript;charset=utf-8;base64," + Base64 . encode ( parser . generate ( ) ) ;
80
+ } ) . removeAttr ( 'disabled' ) ;
81
+
82
+
83
+
84
+ parser . resolutions . forEach ( function ( res ) {
85
+ var r = res [ 2 ] ;
86
+ if ( ! r . bydefault ) return ;
87
+ $ ( "#gen_out" ) . append ( r . msg + "\n" + "(" + r . s + ", " + r . r + ") -> " + r . action ) ;
88
+ } ) ;
89
+
90
+ parser2 = parser . createParser ( ) ;
91
+ }
92
+
93
+ // for newer browsers
94
+ function callWorker ( grammar ) {
95
+ worker . addEventListener ( 'error' , onError ) ;
96
+ worker . addEventListener ( 'message' , function ( e ) {
97
+ onSuccess ( e . data . result ) ;
98
+ } ) ;
99
+
100
+ // ask the web worker to parse the grammar for us
101
+ worker . postMessage ( grammar ) ;
102
+ }
103
+
104
+ // for older browsers (IE <=9, Android <=4.3)
105
+ function callNonWorker ( grammar ) {
106
+ Jison . print = function ( ) { } ;
107
+ var cfg ;
48
108
49
- $ ( "#out" ) . removeClass ( "good" ) . removeClass ( "bad" ) . html ( '' ) ;
50
- $ ( "#gen_out" ) . removeClass ( "good" ) . removeClass ( "bad" ) ;
51
- if ( ! parser . conflicts ) {
52
- $ ( "#gen_out" ) . html ( 'Generated successfully!' ) . addClass ( 'good' ) ;
109
+ try {
110
+ cfg = JSON . parse ( grammar ) ;
111
+ } catch ( e ) {
112
+ try {
113
+ cfg = bnf . parse ( grammar ) ;
114
+ } catch ( e ) {
115
+ return onError ( e ) ;
116
+ }
117
+ }
118
+
119
+ onSuccess ( { cfg : cfg , type : 'lalr' } ) ;
120
+ }
121
+
122
+ $ ( "#gen_out" ) . html ( "Parsing..." )
123
+ . removeClass ( 'good' )
124
+ . removeClass ( 'bad' )
125
+ . addClass ( 'warning' ) ;
126
+ $ ( '#download_btn' ) . attr ( 'disabled' , true ) ;
127
+
128
+ var grammar = $ ( "#grammar" ) . val ( ) ;
129
+
130
+ if ( typeof Worker !== 'undefined' ) {
131
+ callWorker ( grammar ) ;
53
132
} else {
54
- $ ( "#gen_out" ) . html ( 'Conflicts encountered:<br/>' ) . addClass ( 'bad' ) ;
133
+ callNonWorker ( grammar ) ;
55
134
}
56
-
57
- $ ( "#download_btn" ) . click ( function ( ) {
58
- window . location . href = "data:application/javascript;charset=utf-8;base64," + Base64 . encode ( parser . generate ( ) ) ;
59
- } ) . removeAttr ( 'disabled' ) ;
60
-
61
- parser . resolutions . forEach ( function ( res ) {
62
- var r = res [ 2 ] ;
63
- if ( ! r . bydefault ) return ;
64
- $ ( "#gen_out" ) . append ( r . msg + "\n" + "(" + r . s + ", " + r . r + ") -> " + r . action ) ;
65
- } ) ;
66
-
67
- parser2 = parser . createParser ( ) ;
68
135
}
69
136
70
137
function runParser ( ) {
71
- if ( ! parser ) processGrammar ( ) ;
138
+ if ( ! parser ) {
139
+ processGrammar ( ) ;
140
+ }
72
141
printOut ( "Parsing..." ) ;
73
142
var source = $ ( "#source" ) . val ( ) ;
74
143
try {
0 commit comments