Giter Club home page Giter Club logo

dotparser's Introduction

dotparser

Parser of GraphViz dot file format.

Build Status

usage

 var parse = require('dotparser');
 var ast = parse('graph g {}');

 // ast is now an abstract syntax tree of an empty graph:
 // [{
 //   "type": "graph",
 //   "children": [],
 //   "id": "g"
 // }]

why?

The produced output is not bound to any specific graph library. It can be used by graph library authors to transform dot files into their own graph representation.

This implementation is capable of parsing all graphs from standard graphviz test suite.

install

With npm do:

npm install dotparser

compiling grammar

If you've changed grammar and want to have an updated parser, run this:

npm start

This will generate a new parser and save it into grammar/dot.js file

license

MIT

dotparser's People

Contributors

amilajack avatar anvaka avatar fecorreiabr avatar magjac 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

Watchers

 avatar  avatar  avatar  avatar  avatar

dotparser's Issues

importing dot content > 256 mb fails

I can not import dot content> 256 MB due to the string limitation in V8
(according to: https://stackoverflow.com/questions/40382268/error-tostring-failed-node-js-buffer )

My code:

fs.readFile(argv.f, 'utf8', function(err, data) {
 var graph = dot(data);

...

leads to:

dir ./graph_data/FFM/2017-05-26T09:57:20.821Z created
buffer.js:385
    throw new Error('toString failed');
    ^

Error: toString failed
    at Buffer.toString (buffer.js:385:11)
    at FSReqWrap.readFileAfterClose [as oncomplete] (fs.js:378:21)

Any chance for an additional dot(filename) approach instead of dot(string_content) with a streaming line-by-line processing parser for avoiding this buffer limitation?

Thx

Stmt is not full

Please change line

export type Stmt = AttrStmt | EdgeStmt | NodeStmt;

to
export type Stmt = AttrStmt | EdgeStmt | NodeStmt | Subgraph;

I tried using the type hierarchy in Typescript code and I think found a bug.

Anonymous subgraph is not parsed properly

Consider the following graph:

digraph G {
  {}
}

According to the dot language specs the empty brackets should be considered as subgraph, since they satisfy rules:

subgraph    :   [ subgraph [ ID ] ] '{' stmt_list '}'

Square brackets [ and ] enclose optional items. Currently this parser fails to recognize it.

dotparser fails on elaborate node names

while using it with ngraph.fromdot i get following error:

root@master:~/allpuppet # node layout.js ./relationships.dot 
Loading graph from ./relationships.dot

/root/allpuppet/node_modules/ngraph.fromdot/node_modules/dotparser/grammar/dot.js:2853
      throw peg$buildException(null, peg$maxFailExpected, peg$maxFailPos);
            ^
SyntaxError: Expected "--", "->", ";", "<", "=", "[", "\"", "edge", "graph", "node", "subgraph", "{", "}", NUMBER, UNICODE_STRING or port but "." found.

it seems that it tries to parse node name, which is enclosed with quotes.
same with other non-text characters like slashes or dashes.

source dot file comes from puppet and is rendered correctly with graphviz.
it's here: https://gist.github.com/asquelt/785afb4249595f40c235

An empty subgraph generates an incorrect ast

This is an very minor issue in an extreme corner case, but nevertheless...

This dot:

digraph {
    subgraph s1 {}
}

generates:

[
    {
        "type": "digraph",
        "children": [
            {
                "type": "subgraph",
                "id": "s1",
                "children": []
            },
            {
                "type": "subgraph",
                "children": null
            }
        ]
    }
]

So there is two subgraphs in the ast when there should be only one. The first one is correct, but the second is not.

The unnamed subgraph:

digraph {{}}

generates:

[
    {
        "type": "digraph",
        "children": [
            {
                "type": "subgraph",
                "children": null
            }
        ]
    }
]

which contains only the erroneous subgraph.

If a node is added to the named subgraph:

digraph {subgraph s1{a}}

the ast is correct:

[
    {
        "type": "digraph",
        "children": [
            {
                "type": "subgraph",
                "id": "s1",
                "children": [
                    {
                        "type": "node_stmt",
                        "node_id": {
                            "type": "node_id",
                            "id": "a"
                        },
                        "attr_list": []
                    }
                ]
            }
        ]
    }
]

Backport issues found

I ended up copying the dotparser grammar to my own project, since I needed to add line number information and whitespace, newline and comment nodes to the ast and I didn't think that that would be generally useful. During that work I found a couple of issues that I fixed without reporting them here.

You might want to have a look at https://github.com/magjac/graphviz-visual-editor/commits/master/src/dotGrammar.pegjs to see if there are any fixes you'd like to backport to dotparser.

.d.ts

Hi Andrei,
I consume your very useful dotparser in a typescript project. Ideally I need the type files for it.
Can you generate them please?
Thanks,
Lev

big.gv fails

This file is successfully processed by dot.exe. dotparser fails with the message

โœ– Expected """ or "\" but "\n" found.

grammar/dot.js

519 | function peg$buildStructuredError(expected, found, location) {
ror(
| -----------^
peg$SyntaxError.buildMessage(expected, found),

test: big.gv
stack: |
peg$buildStructuredError (grammar/dot.js:520:12)
peg$parse (grammar/dot.js:2938:11)
Test. (test/index.js:9:13)
type: SyntaxError
expected:
- &a1
type: literal
text:
ignoreCase: false
- *a1
- type: literal
text: '"'
ignoreCase: false
found: "\n"
location:
start:
offset: 215
line: 5
column: 50
end:
offset: 216
line: 6
column: 1
tapCaught: testFunctionThrow.

It fails on a long string that begins like that:
struct3 [shape=record,label="hello\nworld\n
and | { Thats the reason | post-menopausal |

The graph is "big.gv" of Graphvis collection. I am not sure how to share it with you, cannot find it on the web at the moment. Let me know if it is a problem and I will share it.

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.