haml-more 0.4.0.a

Sign up to get free protection for your applications and to get access to all the features.
Files changed (225) hide show
  1. data/README.md +79 -0
  2. data/lib/haml/more/coffee_script.rb +137 -0
  3. data/lib/haml/more/content_for.rb +25 -0
  4. data/lib/haml/more.rb +45 -0
  5. data/lib/haml-more.rb +1 -0
  6. data/lib/sass/more.rb +16 -0
  7. data/lib/sass-more.rb +1 -0
  8. data/spec/sass/more_spec.rb +21 -0
  9. data/vendor/coffee-script/Cakefile +57 -0
  10. data/vendor/coffee-script/LICENSE +22 -0
  11. data/vendor/coffee-script/README +41 -0
  12. data/vendor/coffee-script/Rakefile +20 -0
  13. data/vendor/coffee-script/bin/cake +7 -0
  14. data/vendor/coffee-script/bin/coffee +7 -0
  15. data/vendor/coffee-script/documentation/coffee/aliases.coffee +9 -0
  16. data/vendor/coffee-script/documentation/coffee/arguments.coffee +4 -0
  17. data/vendor/coffee-script/documentation/coffee/array_comprehensions.coffee +7 -0
  18. data/vendor/coffee-script/documentation/coffee/assignment.coffee +2 -0
  19. data/vendor/coffee-script/documentation/coffee/cake_tasks.coffee +5 -0
  20. data/vendor/coffee-script/documentation/coffee/comparisons.coffee +5 -0
  21. data/vendor/coffee-script/documentation/coffee/conditionals.coffee +9 -0
  22. data/vendor/coffee-script/documentation/coffee/embedded.coffee +5 -0
  23. data/vendor/coffee-script/documentation/coffee/existence.coffee +8 -0
  24. data/vendor/coffee-script/documentation/coffee/expressions.coffee +9 -0
  25. data/vendor/coffee-script/documentation/coffee/expressions_assignment.coffee +1 -0
  26. data/vendor/coffee-script/documentation/coffee/expressions_comprehension.coffee +3 -0
  27. data/vendor/coffee-script/documentation/coffee/expressions_try.coffee +6 -0
  28. data/vendor/coffee-script/documentation/coffee/fat_arrow.coffee +6 -0
  29. data/vendor/coffee-script/documentation/coffee/functions.coffee +2 -0
  30. data/vendor/coffee-script/documentation/coffee/heredocs.coffee +5 -0
  31. data/vendor/coffee-script/documentation/coffee/multiple_return_values.coffee +5 -0
  32. data/vendor/coffee-script/documentation/coffee/object_comprehensions.coffee +4 -0
  33. data/vendor/coffee-script/documentation/coffee/object_extraction.coffee +13 -0
  34. data/vendor/coffee-script/documentation/coffee/objects_and_arrays.coffee +13 -0
  35. data/vendor/coffee-script/documentation/coffee/overview.coffee +29 -0
  36. data/vendor/coffee-script/documentation/coffee/parallel_assignment.coffee +4 -0
  37. data/vendor/coffee-script/documentation/coffee/range_comprehensions.coffee +6 -0
  38. data/vendor/coffee-script/documentation/coffee/scope.coffee +5 -0
  39. data/vendor/coffee-script/documentation/coffee/slices.coffee +6 -0
  40. data/vendor/coffee-script/documentation/coffee/soaks.coffee +1 -0
  41. data/vendor/coffee-script/documentation/coffee/splats.coffee +25 -0
  42. data/vendor/coffee-script/documentation/coffee/splices.coffee +5 -0
  43. data/vendor/coffee-script/documentation/coffee/strings.coffee +8 -0
  44. data/vendor/coffee-script/documentation/coffee/super.coffee +34 -0
  45. data/vendor/coffee-script/documentation/coffee/switch.coffee +10 -0
  46. data/vendor/coffee-script/documentation/coffee/try.coffee +7 -0
  47. data/vendor/coffee-script/documentation/coffee/while.coffee +10 -0
  48. data/vendor/coffee-script/documentation/css/docs.css +213 -0
  49. data/vendor/coffee-script/documentation/css/idle.css +63 -0
  50. data/vendor/coffee-script/documentation/css/logo.png +0 -0
  51. data/vendor/coffee-script/documentation/index.html.erb +967 -0
  52. data/vendor/coffee-script/documentation/js/aliases.js +14 -0
  53. data/vendor/coffee-script/documentation/js/arguments.js +8 -0
  54. data/vendor/coffee-script/documentation/js/array_comprehensions.js +26 -0
  55. data/vendor/coffee-script/documentation/js/assignment.js +5 -0
  56. data/vendor/coffee-script/documentation/js/cake_tasks.js +14 -0
  57. data/vendor/coffee-script/documentation/js/comparisons.js +5 -0
  58. data/vendor/coffee-script/documentation/js/conditionals.js +12 -0
  59. data/vendor/coffee-script/documentation/js/embedded.js +6 -0
  60. data/vendor/coffee-script/documentation/js/existence.js +7 -0
  61. data/vendor/coffee-script/documentation/js/expressions.js +13 -0
  62. data/vendor/coffee-script/documentation/js/expressions_assignment.js +4 -0
  63. data/vendor/coffee-script/documentation/js/expressions_comprehension.js +12 -0
  64. data/vendor/coffee-script/documentation/js/expressions_try.js +9 -0
  65. data/vendor/coffee-script/documentation/js/fat_arrow.js +15 -0
  66. data/vendor/coffee-script/documentation/js/functions.js +9 -0
  67. data/vendor/coffee-script/documentation/js/heredocs.js +4 -0
  68. data/vendor/coffee-script/documentation/js/intro.js +7 -0
  69. data/vendor/coffee-script/documentation/js/multiple_return_values.js +11 -0
  70. data/vendor/coffee-script/documentation/js/object_comprehensions.js +17 -0
  71. data/vendor/coffee-script/documentation/js/object_extraction.js +17 -0
  72. data/vendor/coffee-script/documentation/js/objects_and_arrays.js +10 -0
  73. data/vendor/coffee-script/documentation/js/overview.js +43 -0
  74. data/vendor/coffee-script/documentation/js/parallel_assignment.js +8 -0
  75. data/vendor/coffee-script/documentation/js/punctuation.js +8 -0
  76. data/vendor/coffee-script/documentation/js/range_comprehensions.js +21 -0
  77. data/vendor/coffee-script/documentation/js/scope.js +10 -0
  78. data/vendor/coffee-script/documentation/js/slices.js +6 -0
  79. data/vendor/coffee-script/documentation/js/soaks.js +4 -0
  80. data/vendor/coffee-script/documentation/js/splats.js +16 -0
  81. data/vendor/coffee-script/documentation/js/splices.js +5 -0
  82. data/vendor/coffee-script/documentation/js/strings.js +9 -0
  83. data/vendor/coffee-script/documentation/js/super.js +37 -0
  84. data/vendor/coffee-script/documentation/js/switch.js +18 -0
  85. data/vendor/coffee-script/documentation/js/try.js +10 -0
  86. data/vendor/coffee-script/documentation/js/while.js +22 -0
  87. data/vendor/coffee-script/documentation/underscore.html +627 -0
  88. data/vendor/coffee-script/examples/beautiful_code/binary_search.coffee +16 -0
  89. data/vendor/coffee-script/examples/beautiful_code/quicksort_runtime.coffee +13 -0
  90. data/vendor/coffee-script/examples/beautiful_code/regular_expression_matcher.coffee +34 -0
  91. data/vendor/coffee-script/examples/blocks.coffee +57 -0
  92. data/vendor/coffee-script/examples/code.coffee +173 -0
  93. data/vendor/coffee-script/examples/computer_science/README +4 -0
  94. data/vendor/coffee-script/examples/computer_science/binary_search.coffee +25 -0
  95. data/vendor/coffee-script/examples/computer_science/bubble_sort.coffee +11 -0
  96. data/vendor/coffee-script/examples/computer_science/linked_list.coffee +106 -0
  97. data/vendor/coffee-script/examples/computer_science/luhn_algorithm.coffee +36 -0
  98. data/vendor/coffee-script/examples/computer_science/merge_sort.coffee +19 -0
  99. data/vendor/coffee-script/examples/computer_science/selection_sort.coffee +23 -0
  100. data/vendor/coffee-script/examples/poignant.coffee +186 -0
  101. data/vendor/coffee-script/examples/potion.coffee +205 -0
  102. data/vendor/coffee-script/examples/underscore.coffee +603 -0
  103. data/vendor/coffee-script/examples/web_server.coffee +12 -0
  104. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Preferences/CoffeeScript.tmPreferences +24 -0
  105. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/Syntaxes/CoffeeScript.tmLanguage +361 -0
  106. data/vendor/coffee-script/extras/CoffeeScript.tmbundle/info.plist +10 -0
  107. data/vendor/coffee-script/extras/EXTRAS +20 -0
  108. data/vendor/coffee-script/extras/coffee.vim +117 -0
  109. data/vendor/coffee-script/index.html +1847 -0
  110. data/vendor/coffee-script/lib/bin/cake +7 -0
  111. data/vendor/coffee-script/lib/bin/coffee +7 -0
  112. data/vendor/coffee-script/lib/cake.js +80 -0
  113. data/vendor/coffee-script/lib/coffee-script.js +61 -0
  114. data/vendor/coffee-script/lib/command_line.js +201 -0
  115. data/vendor/coffee-script/lib/grammar.js +564 -0
  116. data/vendor/coffee-script/lib/lexer.js +405 -0
  117. data/vendor/coffee-script/lib/narwhal.js +44 -0
  118. data/vendor/coffee-script/lib/nodes.js +1328 -0
  119. data/vendor/coffee-script/lib/optparse.js +117 -0
  120. data/vendor/coffee-script/lib/parser.js +536 -0
  121. data/vendor/coffee-script/lib/repl.js +32 -0
  122. data/vendor/coffee-script/lib/rewriter.js +383 -0
  123. data/vendor/coffee-script/lib/scope.js +114 -0
  124. data/vendor/coffee-script/package.json +7 -0
  125. data/vendor/coffee-script/src/cake.coffee +45 -0
  126. data/vendor/coffee-script/src/coffee-script.coffee +45 -0
  127. data/vendor/coffee-script/src/command_line.coffee +130 -0
  128. data/vendor/coffee-script/src/grammar.coffee +456 -0
  129. data/vendor/coffee-script/src/lexer.coffee +327 -0
  130. data/vendor/coffee-script/src/narwhal.coffee +42 -0
  131. data/vendor/coffee-script/src/nodes.coffee +1045 -0
  132. data/vendor/coffee-script/src/optparse.coffee +79 -0
  133. data/vendor/coffee-script/src/repl.coffee +23 -0
  134. data/vendor/coffee-script/src/rewriter.coffee +253 -0
  135. data/vendor/coffee-script/src/scope.coffee +75 -0
  136. data/vendor/coffee-script/test/test_arguments.coffee +34 -0
  137. data/vendor/coffee-script/test/test_array_comprehension.coffee +42 -0
  138. data/vendor/coffee-script/test/test_assignment.coffee +26 -0
  139. data/vendor/coffee-script/test/test_blocks.coffee +4 -0
  140. data/vendor/coffee-script/test/test_calling_super.coffee +42 -0
  141. data/vendor/coffee-script/test/test_chained_calls.coffee +25 -0
  142. data/vendor/coffee-script/test/test_destructuring_assignment.coffee +62 -0
  143. data/vendor/coffee-script/test/test_everything.coffee +29 -0
  144. data/vendor/coffee-script/test/test_exceptions.coffee +2 -0
  145. data/vendor/coffee-script/test/test_existence.coffee +81 -0
  146. data/vendor/coffee-script/test/test_expressions.coffee +30 -0
  147. data/vendor/coffee-script/test/test_fancy_if_statement.coffee +26 -0
  148. data/vendor/coffee-script/test/test_functions.coffee +80 -0
  149. data/vendor/coffee-script/test/test_funky_comments.coffee +25 -0
  150. data/vendor/coffee-script/test/test_heredocs.coffee +46 -0
  151. data/vendor/coffee-script/test/test_lexical_scope.coffee +10 -0
  152. data/vendor/coffee-script/test/test_literals.coffee +56 -0
  153. data/vendor/coffee-script/test/test_nested_comprehensions.coffee +11 -0
  154. data/vendor/coffee-script/test/test_newline_escaping.coffee +6 -0
  155. data/vendor/coffee-script/test/test_operations.coffee +18 -0
  156. data/vendor/coffee-script/test/test_range_comprehension.coffee +20 -0
  157. data/vendor/coffee-script/test/test_ranges_and_slices.coffee +16 -0
  158. data/vendor/coffee-script/test/test_splats.coffee +47 -0
  159. data/vendor/coffee-script/test/test_splices.coffee +5 -0
  160. data/vendor/coffee-script/test/test_switch.coffee +64 -0
  161. data/vendor/coffee-script/test/test_while.coffee +30 -0
  162. data/vendor/coffee-script/vendor/jison/Jakefile +31 -0
  163. data/vendor/coffee-script/vendor/jison/README.md +347 -0
  164. data/vendor/coffee-script/vendor/jison/bin/jison +3 -0
  165. data/vendor/coffee-script/vendor/jison/bin/json2jison +3 -0
  166. data/vendor/coffee-script/vendor/jison/examples/ansic.jison +415 -0
  167. data/vendor/coffee-script/vendor/jison/examples/basic.json +8 -0
  168. data/vendor/coffee-script/vendor/jison/examples/basic2.json +9 -0
  169. data/vendor/coffee-script/vendor/jison/examples/basic2_lex.json +16 -0
  170. data/vendor/coffee-script/vendor/jison/examples/basic_lex.json +15 -0
  171. data/vendor/coffee-script/vendor/jison/examples/calculator.jison +38 -0
  172. data/vendor/coffee-script/vendor/jison/examples/calculator.jisonlex +14 -0
  173. data/vendor/coffee-script/vendor/jison/examples/calculator.json +42 -0
  174. data/vendor/coffee-script/vendor/jison/examples/classy.json +105 -0
  175. data/vendor/coffee-script/vendor/jison/examples/classy_ast.json +126 -0
  176. data/vendor/coffee-script/vendor/jison/examples/dism.json +25 -0
  177. data/vendor/coffee-script/vendor/jison/examples/dism_lr0.json +26 -0
  178. data/vendor/coffee-script/vendor/jison/examples/json.js +80 -0
  179. data/vendor/coffee-script/vendor/jison/examples/json_ast.js +83 -0
  180. data/vendor/coffee-script/vendor/jison/examples/precedence.json +26 -0
  181. data/vendor/coffee-script/vendor/jison/examples/reduce_conflict.json +13 -0
  182. data/vendor/coffee-script/vendor/jison/lib/jison/bnf.js +43 -0
  183. data/vendor/coffee-script/vendor/jison/lib/jison/jisonlex.js +18 -0
  184. data/vendor/coffee-script/vendor/jison/lib/jison/json2jison.js +146 -0
  185. data/vendor/coffee-script/vendor/jison/lib/jison/lexer.js +224 -0
  186. data/vendor/coffee-script/vendor/jison/lib/jison/util/bnf-parser.js +383 -0
  187. data/vendor/coffee-script/vendor/jison/lib/jison/util/lex-parser.js +407 -0
  188. data/vendor/coffee-script/vendor/jison/lib/jison/util/set.js +94 -0
  189. data/vendor/coffee-script/vendor/jison/lib/jison/util/typal.js +90 -0
  190. data/vendor/coffee-script/vendor/jison/lib/jison.js +1414 -0
  191. data/vendor/coffee-script/vendor/jison/package.json +14 -0
  192. data/vendor/coffee-script/vendor/jison/src/bnf.jison +110 -0
  193. data/vendor/coffee-script/vendor/jison/src/bnf.jisonlex +25 -0
  194. data/vendor/coffee-script/vendor/jison/src/bnf.lex.json +24 -0
  195. data/vendor/coffee-script/vendor/jison/src/jisonlex.jison +129 -0
  196. data/vendor/coffee-script/vendor/jison/src/jisonlex.jisonlex +31 -0
  197. data/vendor/coffee-script/vendor/jison/src/jisonlex.lex.json +30 -0
  198. data/vendor/coffee-script/vendor/jison/tests/all-tests.js +8 -0
  199. data/vendor/coffee-script/vendor/jison/tests/grammar/bnf.js +91 -0
  200. data/vendor/coffee-script/vendor/jison/tests/grammar/bnf_parse.js +65 -0
  201. data/vendor/coffee-script/vendor/jison/tests/grammar/grammar-tests.js +10 -0
  202. data/vendor/coffee-script/vendor/jison/tests/grammar/json2jison.js +24 -0
  203. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/ansic.jisonlex +115 -0
  204. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.jisonlex +25 -0
  205. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/bnf.lex.json +24 -0
  206. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.jisonlex +31 -0
  207. data/vendor/coffee-script/vendor/jison/tests/grammar/lex/lex_grammar.lex.json +30 -0
  208. data/vendor/coffee-script/vendor/jison/tests/grammar/lex.jison +119 -0
  209. data/vendor/coffee-script/vendor/jison/tests/grammar/lex.js +58 -0
  210. data/vendor/coffee-script/vendor/jison/tests/grammar/lex_parse.js +117 -0
  211. data/vendor/coffee-script/vendor/jison/tests/lexer/lexer-tests.js +6 -0
  212. data/vendor/coffee-script/vendor/jison/tests/lexer/regexplexer.js +417 -0
  213. data/vendor/coffee-script/vendor/jison/tests/parser/actions.js +311 -0
  214. data/vendor/coffee-script/vendor/jison/tests/parser/api.js +236 -0
  215. data/vendor/coffee-script/vendor/jison/tests/parser/generator.js +196 -0
  216. data/vendor/coffee-script/vendor/jison/tests/parser/lalr.js +183 -0
  217. data/vendor/coffee-script/vendor/jison/tests/parser/lr0.js +72 -0
  218. data/vendor/coffee-script/vendor/jison/tests/parser/lr1.js +119 -0
  219. data/vendor/coffee-script/vendor/jison/tests/parser/parser-tests.js +14 -0
  220. data/vendor/coffee-script/vendor/jison/tests/parser/precedence.js +237 -0
  221. data/vendor/coffee-script/vendor/jison/tests/parser/slr.js +52 -0
  222. data/vendor/coffee-script/vendor/jison/tests/parser/tables.js +126 -0
  223. data/vendor/coffee-script/vendor/jison/tests/performance.js +110 -0
  224. data/vendor/coffee-script/vendor/jison/tests/setup.js +3 -0
  225. metadata +324 -0
@@ -0,0 +1,347 @@
1
+ Jison
2
+ =====
3
+
4
+ An API for creating parsers in JavaScript
5
+ -----------------------------------------
6
+
7
+ Jison generates bottom-up parsers in JavaScript. Its API is similar to Bison's, hence the name. It supports many of Bison's major features, plus some of its own. If you are new to parser generators such as Bison, and Context-free Grammars in general, a [good introduction][1] is found in the Bison manual. If you already know Bison, Jison should be easy to pickup.
8
+
9
+ A brief warning before proceeding: **the API is ridiculously unstable** right now. The goal is to mirror Bison where it makes sense, but we're not even there yet. Also, optimization has not been a main focus as of yet.
10
+
11
+ Briefly, Jison takes a JSON encoded grammar specification and outputs a JavaScript file capable of parsing the language described by that grammar specification. You can then use the generated script to parse inputs and accept, reject, or perform actions based on the input.
12
+
13
+ Installation
14
+ ------------
15
+ **Prerequisite**: To run Jison from the command line, you'll need to have [Narwhal][2] installed and available from your `PATH`.
16
+
17
+ Clone the github repository:
18
+
19
+ git clone git://github.com/zaach/jison.git
20
+
21
+ Usage from the command line
22
+ -----------------------
23
+
24
+ Now you're ready to generate some parsers:
25
+
26
+ cd jison
27
+ narwhal bin/jison examples/calculator.jison examples/calculator.jisonlex
28
+
29
+ This will generate a `calculator.js` file in your current working directory. This file can be used to parse an input file, like so:
30
+
31
+ echo "2^32 / 1024" > testcalc
32
+ narwhal calculator.js testcalc
33
+
34
+ This will print out `4194304`.
35
+
36
+ Usage from a CommonJS module
37
+ --------------------------
38
+
39
+ You can generate parsers programatically from JavaScript as well. Assuming Jison is in your commonjs environment's load path:
40
+
41
+ // mygenerator.js
42
+ var Parser = require("jison").Parser;
43
+
44
+ var grammar = {
45
+ "lex": {
46
+ "rules": [
47
+ ["\\s+", "/* skip whitespace */"],
48
+ ["[a-f0-9]+", "return 'HEX';"]
49
+ ]
50
+ },
51
+
52
+ "bnf": {
53
+ "hex_strings" :[ "hex_strings HEX",
54
+ "HEX" ]
55
+ }
56
+ };
57
+
58
+ var parser = new Parser(grammar);
59
+
60
+ // generate source, ready to be written to disk
61
+ var parserSource = parser.generate();
62
+
63
+ // you can also use the parser directly from memory
64
+
65
+ // returns true
66
+ parser.parse("adfe34bc e82a");
67
+
68
+ // throws lexical error
69
+ parser.parse("adfe34bc zxg");
70
+
71
+ Using the generated parser
72
+ --------------------------
73
+ So, you have generated your parser through the command line or JavaScript API and have saved it to disk. Now it can be put to use.
74
+
75
+ As demonstrated before, the parser can be used from the command line:
76
+
77
+ narwhal calculator.js testcalc
78
+
79
+ Though, more ideally, the parser will be a dependency of another module. You can require it from another module like so:
80
+
81
+ // mymodule.js
82
+ var parser = require("./calculator").parser;
83
+
84
+ function exec (input) {
85
+ return parser.parse(input);
86
+ }
87
+
88
+ var twenty = exec("4 * 5");
89
+
90
+ Or more succinctly:
91
+
92
+ // mymodule.js
93
+ function exec (input) {
94
+ return require("./calculator").parse(input);
95
+ }
96
+
97
+ var twenty = exec("4 * 5");
98
+
99
+ Using the parser in a web page
100
+ ----------------------------
101
+
102
+ The generated parser script may be included in a web page without any need for a CommonJS loading environment. It's as simple as pointing to it via a scipt tag:
103
+
104
+ <script src="calc.js"></script>
105
+
106
+ When you generate the parser, you can specify the variable name it will be declared as:
107
+
108
+ // mygenerator.js
109
+ var parserSource = generator.generate({moduleName: "calc"});
110
+ // then write parserSource to a file called, say, calc.js
111
+
112
+ Whatever `moduleName` you specified will be the the variable you can access the parser from in your web page:
113
+
114
+ <!-- mypage.html -->
115
+ ...
116
+ <script src="calc.js"></script>
117
+ <script>
118
+ calc.parse("42 / 0");
119
+ </script>
120
+ ...
121
+
122
+ The moduleName you specify can also include a namespace, e.g:
123
+ // mygenerator.js
124
+ var parserSource = parser.generate({moduleName: "myCalculator.parser"});
125
+
126
+ And could be used like so:
127
+
128
+ <!-- mypage.html -->
129
+ ...
130
+ <script>
131
+ var myCalculator = {};
132
+ </script>
133
+ <script src="calc.js"></script>
134
+ <script>
135
+ myCalculator.parser.parse("42 / 0");
136
+ </script>
137
+ ...
138
+
139
+ Or something like that -- you get the picture.
140
+
141
+ A demo of the calculator script used in a web page is [here](http://zaach.github.com/jison/demo/calc.html) and the source of the page and the narwhal script to generate the parser are [here](http://gist.github.com/265842).
142
+
143
+ Specifying a language
144
+ ---------------------
145
+ The process of parsing a language involves two phases: **lexical analysis** (tokenizing) and **parsing**, which the Lex/Yacc and Flex/Bison combinations are famous for. Jison lets you specify a parser much like you would using Bison/Flex, with separate files for tokenization rules and for the language grammar.
146
+
147
+ For example, here is the calculator parser:
148
+
149
+ calc.jisonlex, tokenization rules
150
+
151
+ %%
152
+ \s+ {/* skip whitespace */}
153
+ [0-9]+("."[0-9]+)?\b {return 'NUMBER';}
154
+ "*" {return '*';}
155
+ "/" {return '/';}
156
+ "-" {return '-';}
157
+ "+" {return '+';}
158
+ "^" {return '^';}
159
+ "(" {return '(';}
160
+ ")" {return ')';}
161
+ "PI" {return 'PI';}
162
+ "E" {return 'E';}
163
+ <<EOF>> {return 'EOF';}
164
+
165
+ and calc.jison, language grammar
166
+
167
+ /* description: Grammar for a parser that parses and executes mathematical expressions. */
168
+
169
+ %left '+' '-'
170
+ %left '*' '/'
171
+ %left '^'
172
+ %left UMINUS
173
+
174
+ %%
175
+
176
+ S
177
+ : e EOF
178
+ {print($1); return $1;}
179
+ ;
180
+
181
+ e
182
+ : e '+' e
183
+ {$$ = $1+$3;}
184
+ | e '-' e
185
+ {$$ = $1-$3;}
186
+ | e '*' e
187
+ {$$ = $1*$3;}
188
+ | e '/' e
189
+ {$$ = $1/$3;}
190
+ | e '^' e
191
+ {$$ = Math.pow($1, $3);}
192
+ | '-' e
193
+ {$$ = -$2;} %prec UMINUS
194
+ | '(' e ')'
195
+ {$$ = $2;}
196
+ | NUMBER
197
+ {$$ = Number(yytext);}
198
+ | E
199
+ {$$ = Math.E;}
200
+ | PI
201
+ {$$ = Math.PI;}
202
+ ;
203
+
204
+ which compiles down to this JSON:
205
+
206
+ {
207
+ "lex": {
208
+ "rules": [
209
+ ["\\s+", "/* skip whitespace */"],
210
+ ["[0-9]+(?:\\.[0-9]+)?\\b", "return 'NUMBER';"],
211
+ ["\\*", "return '*';"],
212
+ ["\\/", "return '/';"],
213
+ ["-", "return '-';"],
214
+ ["\\+", "return '+';"],
215
+ ["\\^", "return '^';"],
216
+ ["\\(", "return '(';"],
217
+ ["\\)", "return ')';"],
218
+ ["PI\\b", "return 'PI';"],
219
+ ["E\\b", "return 'E';"],
220
+ ["$", "return 'EOF';"]
221
+ ]
222
+ },
223
+
224
+ "operators": [
225
+ ["left", "+", "-"],
226
+ ["left", "*", "/"],
227
+ ["left", "^"],
228
+ ["left", "UMINUS"]
229
+ ],
230
+
231
+ "bnf": {
232
+ "S" :[[ "e EOF", "print($1); return $1;" ]],
233
+
234
+ "e" :[[ "e + e", "$$ = $1+$3;" ],
235
+ [ "e - e", "$$ = $1-$3;" ],
236
+ [ "e * e", "$$ = $1*$3;" ],
237
+ [ "e / e", "$$ = $1/$3;" ],
238
+ [ "e ^ e", "$$ = Math.pow($1, $3);" ],
239
+ [ "- e", "$$ = -$2;", {"prec": "UMINUS"} ],
240
+ [ "( e )", "$$ = $2;" ],
241
+ [ "NUMBER", "$$ = Number(yytext);" ],
242
+ [ "E", "$$ = Math.E;" ],
243
+ [ "PI", "$$ = Math.PI;" ]]
244
+ }
245
+ }
246
+
247
+ Jison accepts both the Bison/Flex style formats, or the raw JSON format, e.g:
248
+
249
+ narwhal bin/jison examples/calculator.jison examples/calculator.jisonlex
250
+ or
251
+ narwhal bin/jison examples/calculator.json
252
+
253
+ More examples can be found in the `examples/` and `tests/parser/` directories.
254
+
255
+ Sharing scope
256
+ ------------
257
+
258
+ In Bison, code is expected to be lexically defined within the scope of the semantic actions. E.g., chunks of code may be included in the generated parser source, which are available from semantic actions.
259
+
260
+ Jison is more modular. Instead of pulling code into the generated module, the generated module is expected to be required and used by other modules. This means that if you want to expose functionality to the semantic actions, you can't rely on it being available through lexical scoping. Instead, the parser has a `yy` property which is exposed to actions as the `yy` free variable. Any functionality attached to this property is available in both lexical and semantic actions through the `yy` free variable.
261
+
262
+ An example from orderly.js:
263
+
264
+ var parser = require("./orderly/parse").parser;
265
+
266
+ // set parser's shared scope
267
+ parser.yy = require("./orderly/scope");
268
+
269
+ // returns the JSON object
270
+ var parse = exports.parse = function (input) {
271
+ return parser.parse(input);
272
+ };
273
+ ...
274
+
275
+ The `scope` module contains logic for building data structures, which is used within the semantic actions.
276
+
277
+ *TODO: More on this.*
278
+
279
+ Lexical Analysis
280
+ ----------------
281
+ Jison includes a rather rudimentary lexer, though **any module that supports the basic lexer API could be used** in its place. Jison's lexer uses the `lex` key of the JSON grammar spec, where the rules for matching a token are defined along with the action to execute on a match. Usually, the action will return the token which is used by the Jison parser. A custom lexer could be used instead with it's own methods of tokenizing.
282
+
283
+ *TODO: More on this.*
284
+
285
+ Parsing algorithms
286
+ ------------------
287
+ Like Bison, Jison can recognize languages described by LALR(1) grammars, though it also has modes for LR(0), SLR(1), and LR(1). It also has a special mode for generating LL(1) parse tables (requested by my professor,) and could be extended to generate a recursive descent parser for LL(k) languages in the future. But, for now, Jison is geared toward bottom-up parsing.
288
+
289
+ **LR(1) mode is currently not practical for use with anything other than toy grammars, but that is entirely a consequence of the algorithm used, and may change in the future.*
290
+
291
+ Real world example
292
+ ------------------
293
+
294
+ I wrote a parser for [Orderly][3] using Jison. Some benefits I found were:
295
+
296
+ - If modeled after the normative language grammar, it is guaranteed to recognize the correct language.
297
+ - Adding new syntax is straight forward.
298
+ - It was much faster to develop than if I were to attempt implementing a (top-down) parser from scratch. But for others not used to grammar specifications, this might not be the case.
299
+
300
+ Contributors
301
+ ------------
302
+ - Zach Carter
303
+ - Jarred Ligatti
304
+ - Manuel E. Bermúdez
305
+
306
+ License
307
+ -------
308
+
309
+ > Copyright (c) 2009 Zachary Carter
310
+ >
311
+ > Permission is hereby granted, free of
312
+ > charge, to any person obtaining a
313
+ > copy of this software and associated
314
+ > documentation files (the "Software"),
315
+ > to deal in the Software without
316
+ > restriction, including without
317
+ > limitation the rights to use, copy,
318
+ > modify, merge, publish, distribute,
319
+ > sublicense, and/or sell copies of the
320
+ > Software, and to permit persons to
321
+ > whom the Software is furnished to do
322
+ > so, subject to the following
323
+ > conditions:
324
+ >
325
+ > The above copyright notice and this
326
+ > permission notice shall be included
327
+ > in all copies or substantial portions
328
+ > of the Software.
329
+ >
330
+ > THE SOFTWARE IS PROVIDED "AS IS",
331
+ > WITHOUT WARRANTY OF ANY KIND, EXPRESS
332
+ > OR IMPLIED, INCLUDING BUT NOT LIMITED
333
+ > TO THE WARRANTIES OF MERCHANTABILITY,
334
+ > FITNESS FOR A PARTICULAR PURPOSE AND
335
+ > NONINFRINGEMENT. IN NO EVENT SHALL THE
336
+ > AUTHORS OR COPYRIGHT HOLDERS BE
337
+ > LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
338
+ > LIABILITY, WHETHER IN AN ACTION OF
339
+ > CONTRACT, TORT OR OTHERWISE, ARISING
340
+ > FROM, OUT OF OR IN CONNECTION WITH THE
341
+ > SOFTWARE OR THE USE OR OTHER DEALINGS
342
+ > IN THE SOFTWARE.
343
+
344
+
345
+ [1]: http://dinosaur.compilertools.net/bison/bison_4.html
346
+ [2]: http://github.com/280north/narwhal
347
+ [3]: http://github.com/zaach/orderly.js
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env narwhal
2
+
3
+ require('jison').main(system.args);
@@ -0,0 +1,3 @@
1
+ #!/usr/bin/env narwhal
2
+
3
+ require('jison/json2jison').main(system.args);