Permalink
Browse files

Initial commit.

  • Loading branch information...
0 parents commit 152426cd1f7b68f8c130920578b44bc7463de648 @alxlit committed May 16, 2011
Showing with 13,436 additions and 0 deletions.
  1. +22 −0 LICENSE
  2. +44 −0 README.markdown
  3. +36 −0 build/coffeescript.php
  4. +15 −0 build/coffeescript/errors.php
  5. +103 −0 build/coffeescript/helpers.php
  6. +1,081 −0 build/coffeescript/lexer.php
  7. +116 −0 build/coffeescript/nodes.php
  8. +30 −0 build/coffeescript/nodes/access.php
  9. +67 −0 build/coffeescript/nodes/arr.php
  10. +9 −0 build/coffeescript/nodes/assign.php
  11. +229 −0 build/coffeescript/nodes/base.php
  12. +223 −0 build/coffeescript/nodes/block.php
  13. +254 −0 build/coffeescript/nodes/call.php
  14. +206 −0 build/coffeescript/nodes/class.php
  15. +9 −0 build/coffeescript/nodes/closure.php
  16. +9 −0 build/coffeescript/nodes/code.php
  17. +35 −0 build/coffeescript/nodes/comment.php
  18. +9 −0 build/coffeescript/nodes/existence.php
  19. +25 −0 build/coffeescript/nodes/extends.php
  20. +9 −0 build/coffeescript/nodes/for.php
  21. +9 −0 build/coffeescript/nodes/if.php
  22. +9 −0 build/coffeescript/nodes/in.php
  23. +25 −0 build/coffeescript/nodes/index.php
  24. +78 −0 build/coffeescript/nodes/literal.php
  25. +100 −0 build/coffeescript/nodes/obj.php
  26. +9 −0 build/coffeescript/nodes/op.php
  27. +9 −0 build/coffeescript/nodes/param.php
  28. +9 −0 build/coffeescript/nodes/parens.php
  29. +9 −0 build/coffeescript/nodes/push.php
  30. +125 −0 build/coffeescript/nodes/range.php
  31. +53 −0 build/coffeescript/nodes/return.php
  32. +45 −0 build/coffeescript/nodes/slice.php
  33. +9 −0 build/coffeescript/nodes/splat.php
  34. +9 −0 build/coffeescript/nodes/switch.php
  35. +9 −0 build/coffeescript/nodes/throw.php
  36. +9 −0 build/coffeescript/nodes/try.php
  37. +207 −0 build/coffeescript/nodes/value.php
  38. +9 −0 build/coffeescript/nodes/while.php
  39. +3,249 −0 build/coffeescript/parser.php
  40. +13 −0 build/coffeescript/rewriter.php
  41. 0 build/coffeescript/scope.php
  42. +90 −0 make.php
  43. +274 −0 src/grammar.y
  44. +78 −0 test/arrays.coffee
  45. +27 −0 test/index.php
  46. +10 −0 vendor/ParserGenerator/LICENSE.txt
  47. +937 −0 vendor/ParserGenerator/PHP/Lempar.php
  48. +257 −0 vendor/ParserGenerator/PHP/ParserGenerator/Action.php
  49. +299 −0 vendor/ParserGenerator/PHP/ParserGenerator/ActionTable.php
  50. +574 −0 vendor/ParserGenerator/PHP/ParserGenerator/Config.php
  51. +1,857 −0 vendor/ParserGenerator/PHP/ParserGenerator/Data.php
  52. +851 −0 vendor/ParserGenerator/PHP/ParserGenerator/Parser.php
  53. +126 −0 vendor/ParserGenerator/PHP/ParserGenerator/PropagationLink.php
  54. +144 −0 vendor/ParserGenerator/PHP/ParserGenerator/Rule.php
  55. +283 −0 vendor/ParserGenerator/PHP/ParserGenerator/State.php
  56. +288 −0 vendor/ParserGenerator/PHP/ParserGenerator/Symbol.php
  57. +5 −0 vendor/ParserGenerator/PHP/ParserGenerator/cli.php
  58. +811 −0 vendor/ParserGenerator/ParserGenerator.php
22 LICENSE
@@ -0,0 +1,22 @@
+Copyright (c) 2011 Jeremy Ashkenas, Alex Little
+
+Permission is hereby granted, free of charge, to any person
+obtaining a copy of this software and associated documentation
+files (the "Software"), to deal in the Software without
+restriction, including without limitation the rights to use,
+copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the
+Software is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
+OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
+HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,44 @@
+
+# CoffeeScript-PHP
+
+A port of the [CoffeeScript](http://jashkenas.github.com/coffee-script/)
+*compiler* to PHP. It's incomplete, porting is really tedious so feel free to
+contribute.
+
+### Complete
+
+* Grammar (src/grammar.y) for the parser generator (using Lemon, since there's
+ no port of Bison to PHP).
+* Lexer (build/coffeescript/lexer.php).
+* Some of the nodes (15/31) (build/coffeescript/nodes/).
+
+The nodes are the focus at the moment. Right now I'm just blindly porting them
+to PHP, once that's done they'll undoubtedly need tweaking.
+
+### Todo
+
+* Scope manager.
+* Rewriter.
+* Test cases. I figure these will just compare to references created by the
+ original compiler, nothing fancy.
+
+## FAQ
+
+### Why not modify the original compiler to emit PHP?
+
+Much more work to do... There are too many differences between JavaScript and
+PHP (object model, underlying libraries, etc) that would need to be seriously
+thought about. There's also less incentive/reason to do stuff like that server-side.
+
+## Requirements
+
+PHP 5.3+ (uses namespaces, anonymous functions).
+
+## Usage
+
+In your projects,
+
+```php
+$js = CoffeeScript\compile('path/to/source.coffee');
+```
+
@@ -0,0 +1,36 @@
+<?php
+
+namespace CoffeeScript;
+
+require 'coffeescript/lexer.php';
+require 'coffeescript/parser.php';
+
+/**
+ */
+function compile($source, $options = array())
+{
+ $options = (object) array_merge(array(
+ 'indent' => 0,
+ 'index' => 0,
+ 'line' => 0,
+ 'rewrite' => TRUE
+ ),
+ $options);
+
+ $lexer = new Lexer(file_get_contents($source), $options);
+ $parser = new Parser();
+
+ // While not strictly necessary to do it like this, it's in keeping with the
+ // Lemon documentation to do so.
+ while ($lexer->tokenize())
+ {
+ $parser->parse($lexer->tag(), $lexer->value());
+ }
+
+ // Signal end-of-input to the parser.
+ $parser->parse(0, 0);
+
+ return $parser->yystack;
+}
+
+?>
@@ -0,0 +1,15 @@
+<?php
+
+namespace CoffeeScript;
+
+class Error extends \Exception
+{
+ function __construct($message)
+ {
+ $this->message = $message.'.';
+ }
+}
+
+class SyntaxError extends Error {}
+
+?>
@@ -0,0 +1,103 @@
+<?php
+
+namespace CoffeeScript;
+
+function compact(array $array)
+{
+ $compacted = array();
+
+ foreach ($array as $k => $v)
+ {
+ if ($v)
+ {
+ $compacted[] = $v;
+ }
+ }
+
+ return $compacted;
+}
+
+function del( & $obj, $key)
+{
+ $val = NULL;
+
+ if (isset($obj[$key]))
+ {
+ $val = $obj[$key];
+ unset($obj[$key]);
+ }
+
+ return $val;
+}
+
+function flatten(array $array)
+{
+ $flattened = array();
+
+ foreach ($array as $k => $v)
+ {
+ if (is_array($v))
+ {
+ $flattened = array_merge($flattened, flatten($v));
+ }
+ else
+ {
+ $flattened[] = $v;
+ }
+ }
+
+ return $flattened;
+}
+
+function last($array, $back = 0)
+{
+ $i = count($array) - $back - 1;
+
+ return isset($array[$i]) ? $array[$i] : NULL;
+}
+
+/**
+ * In Jison, token types (tags) are represented simply using strings, whereas
+ * with ParserGenerator (a port of Lemon) we're stuck with using numeric
+ * constants for each type.
+ *
+ * This function maps those string representations to their numeric constants,
+ * making it easier to port directly from the CoffeeScript source.
+ */
+function t($name)
+{
+ static $map = array(
+ '.' => 'ACCESSOR',
+ '&' => 'AMPERSAND',
+ '[' => 'ARRAY_START',
+ ']' => 'ARRAY_END',
+ ':' => 'COLON',
+ '?.' => 'EXISTENTIAL_ACCESSOR',
+ '(' => 'PAREN_START',
+ ')' => 'PAREN_END',
+ '::' => 'PROTOTYPE'
+ );
+
+ if (func_num_args() > 1)
+ {
+ $name = func_get_args();
+ }
+
+ if ((func_num_args() > 1 && $name = func_get_args()) || is_array($name))
+ {
+ $tags = array();
+
+ foreach ($name as $v)
+ {
+ $tags[] = t($v);
+ }
+
+ return $tags;
+ }
+
+ $name = 'Parser::YY_'.(isset($map[$name]) ? $map[$name] : $name);
+
+ return defined($name) ? constant($name) : NULL;
+}
+
+?>
Oops, something went wrong.

0 comments on commit 152426c

Please sign in to comment.