Giter VIP home page Giter VIP logo

coffeescript-php's Introduction

CoffeeScript PHP

A port of the CoffeeScript compiler to PHP.

Status

CoffeeScript version 1.3.1 has been fully ported over (see tags). There are a couple benign differences between the port's compiled code and the reference's (for example #11), otherwise they match 100%.

Requirements

PHP 5.3+ (uses namespaces, anonymous functions).

Install

It's recommended that you use Composer to install and autoload CoffeeScript. Alternatively you can load it manually:

<?php

require 'vendor/CoffeeScript/Init.php';

// Load manually
CoffeeScript\Init::load();

?>

Usage

The API is really basic (single compile($coffee, $options = NULL) function). I don't plan on expanding it further (keeping it simple). Here are the available options:

  • filename - The source filename, formatted into error messages
  • header - Add a "Generated by..." header
  • bare - If set to true, the generated Javascript won't be embedded in a IIFE
  • rewrite - Enable the rewriter (debugging)
  • tokens - Reference to token stream (debugging)
  • trace - File to write parser trace to (debugging)
<?php

$file = 'path/to/source.coffee';

try
{
  $coffee = file_get_contents($file);

  // See available options above.
  $js = CoffeeScript\Compiler::compile($coffee, array('filename' => $file));
}
catch (Exception $e)
{
  echo $e->getMessage();
}

?>

Development

To rebuild the parser run php make.php. Tests are run in the browser; simply clone the repository somewhere Apache can see it and navigate to tests/.

FAQ

What was the motivation for this project?

I was using PHP a lot at the time and wanted to use, learn more about, and potentially contribute to the CoffeeScript project. I thought it'd be nice to have a native version gave it a shot.

Why not modify the original compiler to emit PHP?

For a number of reasons... First, I don't know why you'd want something like that. If you find PHP intolerable, just don't use it... Second, the original compiler depends on Jison, which is written in JavaScript, so you'd have to do something about that. Third, I think it'd be much more work to try and sort out all the differences between JavaScript and PHP (object model, core classes and libraries, etc).

What parser generator are you using?

Since there's no PHP port of Bison (which the reference compiler uses), we're using a port of Lemon called ParserGenerator.

It's included locally since the PEAR package is unmaintained and seems to be broken. In addition, some minor changes have been made to the parser template (Lempar.php) and the actual generator.

coffeescript-php's People

Contributors

alxlit avatar bfanger avatar briannesbitt avatar cschramm avatar jkrecek avatar kochb avatar olivierphi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

coffeescript-php's Issues

Add a composer.json and use its autoload.

Hi ! first of all, thanks for you very very very very nice work, it's amazing and (very ?) useful !

I'd like to know if you can consider adding a package.json file, it makes life easier for us developer. if you don't know about that amazing tool, you can look at getcomposer.org and packagist.com. It can also be used to manage autoloading (but it requires php 5.3.2).

Anyway, thanks for your work !

Some errors with coffeescript 1.4.0

Since version 1.4.0 of coffeescript is released i got some errors on the testcases...

i do not know if this errors are relevant for the compiled JS...

What's your opinion alxlit ?

Strict Errors with function declarations in nodes/base

I am getting a E_Strict exception on a bunch of the methods on node classes thes first being yy_Access::constructor() not matching yy_Base::constructor().

The exact error(in kohana format) is ErrorException [ Strict ]: Declaration of CoffeeScript\yy_Access::constructor() should be compatible with CoffeeScript\yy_Base::constructor()

I am running Zend Server 5.6
PHP 5.4
OS X Lion

Am trying to modify the code myself but wanted to bring it to your attention

Also would like to say I love the project and thanks for all the work you put into it.

Version 1.8.0 support

Is there any chance for this tool to support newer versions of coffeescript? The function chaining syntax has changed since 1.7.0, for example this:

$ '<div>'
    .appendTo foo

used to mean $('<div>'.appendTo(foo)), but since 1.7.0 it means $('<div>').appendTo(foo)

Fails functions.coffee, function_invocations.coffee for 1.3.1

The test cases functions.coffee and function_invocations.coffee both fail for the 1.3.1 port of CoffeeScript, however this is due to a bug in the reference 1.3.1 compiler that has been fixed in the port, I think, (and in versions 1.3.2+ of the reference).

Here's the problem:

(splat..., @prop) ->

The reference 1.3.1 compiler will produce...

(function() {
  var __slice = [].slice;

  (function() {
    var splat, _i;
    splat = 2 <= arguments.length ? __slice.call(arguments, 0, _i = arguments.length - 1) : (_i = 0, []), prop = arguments[_i++];
    this.prop = prop;
  });
}).call(this);

...which fails to declare the prop variable. The port will correctly declare it, var prop, splat, _i;

Compilation error. 500 Internal server

Here is the script:

Ext.define "Some.Ext.class",
  initComponent: ->
    if "fdsfds"
      alert "fdsf"
      alert "fdsfds"

There is 500 error after that code compilation.

Project Status

What's the status on this project @alxlit? Has it reached a stand still as I'm curious about implementing it for a project where I'd like to avoid depending on the original compilers by default.

Cheers mate.

An option to disable the top self invoking function ?

Hi !

Would it be complex to add a boolean option to control whether the generated Javascript is embedded in a self invoking function or not ? Currently this option doesn't exist, and sometimes it can be annoying to have this top self invoking function (for example, in a RequireJS application, this function is not needed).
(the JS Coffeescript compiler has chosen the bare option name for this functionnality)

Anyway, thanks for this great Coffeescript PHP library !

global variable $UTILITIES in classes/nodes.php

Besides just being nasty, this may cause trouble, since if the file is not included in global context, $UTILITIES will not make it into global namespace and therefore won't be available in the utility function.

This means, if someone - let's say it was me - loads coffeescript from within a class method, all the utilities are left out like in var __indexOf=; which will lead to a JavaScript parse error.

Blank or comment only file

When I try to compile a blank file or a comment only file I get an error. Says:

unexpected YY_TERMINATOR

How to use this ?

Hello,

How to create php code from coffee script ?

I want to use it, but how to steps needed.

How can I convert coffee script to php via command line ?

Kindly update me on this.

Thanks,

Block comments within object declarations sometimes compile incorrectly

Placing a block comment in the middle of an object declaration sometimes breaks the coffeescript-php output, while the official coffee compiler handles them just fine.

Sample Coffee file

App.extend

    hello: ->
        "hi"

    ###
    Where Am I?

    This block comment will trigger a compiler bug.
    ###
    where_am_i: (location) ->
        "oh no!"

Result

(function() {
  App.extend({
    hello: function() {
      return "hi";
    }
  }, (function() {
    /*
        Where Am I?
    
        This block comment will trigger a compiler bug.
        */
  })(), {
    where_am_i: function(location) {
      return "oh no!";
    }
  });
}).call(this);

Reference Rendering, generated by coffee

(function() {

  App.extend({
    hello: function() {
      return "hi";
    },
    /*
        Where Am I?
    
        This block comment will trigger a compiler bug.
    */
    where_am_i: function(location) {
      return "oh no!";
    }
  });

}).call(this);

No pressing need for this to be fixed on my part, just reporting it. Nice work so far.

Regression on master?

Hi there,

I recently tried out the current master to run some of our code on PHP7. I noticed a regression related to one of our source files. While being related to PHP7 the master version also fails to compile my code on PHP5.6

The reduced test case looks like this:

console.log 'foo' unless max > 0

Funny enough, this works:

console.log 'foo' unless max > 0#

I also tried 1.3.1 on PHP7 with the last commit 0dd2727 manually applied ('!==' to '!=' in Lexer.php). Same result.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.