fkling / astexplorer

A web tool to explore the ASTs generated by various parsers.
https://astexplorer.net/
MIT License
6.19k stars 737 forks source link

Integration server side parser ? #222

Open DragorWW opened 7 years ago

DragorWW commented 7 years ago

For example, I use PHP-Parser on PHP and create astexplorer from ajax and php backend

// php parser
import defaultParserInterface from '../utils/defaultParserInterface';

const ID = 'php-ast-parser';

export default {
  ...defaultParserInterface,
  id: ID,
  displayName: ID,
  version: '7.1',
  _ignoredProperties: new Set([]),
  locationProps: new Set(['attributes']),
  nodeToRange({attributes} = {}) {
    if (attributes) {
      return [attributes.startFilePos, attributes.endFilePos+1];
    }
  },
  getNodeName(node) {
    return node.nodeType;
  },
  loadParser(callback) {
    callback();
  },
  opensByDefault(node, key) {
    return key === 'stmts';
  },
  parse(a, code) {
    return fetch('/index.php',{
      method: 'post',
      body: code,
    })
      .then(function(response) {
        return response.json();
       });
  },
};

how to integrate server side parse in astexplorer.net?

fkling commented 7 years ago

Sorry for not having replied yet!

First of all: This is really great, thank you for doing that!

Initially I thought, since you already have it running on a heroku server, if it would just expose an endpoint for parsing PHP, we could use that. However, otoh I don't want to send everybodys code to some external server.

Since this is the first actual implementation for a server side language, there isn't a solution for this yet. However, I was thinking if it's possible to encapsulate all the server logic into a docker container. The container would receive the code and return the AST (as JSON I guess). The server would start and communicate with the container. There are a lot of open questions around this (e.g. should the container run all the time or should it be started per request, how does the server communicate with the container), but I believe this to be a maintainable approach in the long run (we would do the same for other server side languages).

Thoughts?

DragorWW commented 7 years ago

In server-side parser one problem: need backend. Heroku server will stop my container after no calls within 30 minutes.

Docker good idea for creating an abstract container with server-side parser logics.

We can store in repository docker.yml file for difference server-side parsers.

I thin need:

I could prepare docker config for PHP-Parser.

// json api example
// body string
// request
POST server.net/parser/<ParserLangName>
<bodyStringCode>

// response
// json ast
gabro commented 7 years ago

This would be really cool and it could enable so many powerful features. @fkling any thoughts on the strawman proposal above?

RReverser commented 7 years ago

However, otoh I don't want to send everybodys code to some external server.

That's what I'm going to do for one other parser soon :P I don't think it's a bad idea if reusing other open-source and public services that have similar purposes.

fkling commented 7 years ago

@fkling any thoughts on the strawman proposal above?

That seems all reasonable to me. Not sure yet how the server would communicate with the container, but we have to start somewhere.

I don't think it's a bad idea if reusing other open-source and public services that have similar purposes.

No of course not. If it's a trustable resource then we should use it. But I don't want to rely on someones private instance. And thinking long-term, being able to spin up arbitrary server side parsers will make things easier.

RReverser commented 7 years ago

But I don't want to rely on someones private instance.

Ah yeah agreed.

DragorWW commented 7 years ago

@fkling I think we can write to heroku, or similar products for hosting open source project

fkling commented 7 years ago

@DragorWW: Sure, but astexplorer already runs on its own server anyway, so running docker containers there would probably be easier for us in the long run.