kach / nearley

📜🔜🌲 Simple, fast, powerful parser toolkit for JavaScript.
https://nearley.js.org
MIT License
3.61k stars 231 forks source link

Complexity of parsing indented comments #25

Closed knuton closed 10 years ago

knuton commented 10 years ago

I commented out a block of lines for debugging purposes and ran into some strange behaviour: the complexity of parsing a parser definition seems to grow dramatically with the number and indentation level of subsequent indented comments.

$ node_modules/.bin/nearleyc --version
0.2.2

For example, with a series of files named indentn.ne, where a block of comments is indented n spaces, i.e.

$ cat indent0.ne
foo -> "bar"
#1
#2
#3
#4
#5
#6
$ cat indent2.ne
foo -> "bar"
  #1
  #2
  #3
  #4
  #5
  #6

etc., execution time shoots up rapidly:

$ time node_modules/.bin/nearleyc indent0.ne > /dev/null
real    0m0.116s
user    0m0.096s
sys     0m0.019s
$ time node_modules/.bin/nearleyc indent2.ne > /dev/null
real    0m0.763s
user    0m0.720s
sys     0m0.048s
$ time node_modules/.bin/nearleyc indent4.ne > /dev/null
real    0m8.349s
user    0m8.138s
sys     0m0.256s

time node_modules/.bin/nearleyc indent6.ne > /dev/null has not yet finished. :wink:

kach commented 10 years ago

Yikes. I fixed it in the latest commits. npm update and see if the bug persists.

Thanks for reporting this. Out of curiosity, what are you using nearley for? :-)

knuton commented 10 years ago

Looking good, thanks for the quick fix!

$ time node_modules/.bin/nearleyc indent6.ne > /dev/null
real    0m0.124s
user    0m0.097s
sys     0m0.020s

I am toying around with various type checking approaches for my Master's thesis so I need to implement parsers for some simple applicative languages, nothing too fancy. @iarna mentioned nearley in a recent JavaScript Jabber episode, so I thought I'd give it a try.