instaparse

If you're not trampolining your parser, why bother getting up in the morning?
2018-04-18T23:36:17.000017Z

can instaparse be used to parse significant-whitespace-indentation-things, in the python sense?

aengelberg 2018-04-18T23:37:24.000252Z

not really, primarily because of how the "levels" work in said indentation-heavy languages

2018-04-18T23:38:43.000231Z

@aengelberg can you change your username to ængelberg

2018-04-18T23:39:06.000347Z

thanks

aengelberg 2018-04-18T23:39:07.000148Z

done

2018-04-18T23:40:00.000345Z

okay well that's disappointing because I'm parsing such a thing and I guess I'll have to do it tediously by hand have you seen any general tools that can handle it?

aengelberg 2018-04-18T23:41:00.000045Z

if you could somehow pre-tokenize all of the indentation before passing it to instaparse, that might work

aengelberg 2018-04-18T23:43:05.000078Z

what I mean is that you'd have to convert

def f(x):
  if x == 1:
    return 2;
  else:
    return 3;
into
def f(x):
→if x == 1:
→return 2;
←else:
→return 3;
←←

aengelberg 2018-04-18T23:43:32.000188Z

if that makes sense

2018-04-18T23:43:42.000232Z

and the arrows act like brackets?

aengelberg 2018-04-18T23:43:47.000053Z

basically

aengelberg 2018-04-18T23:44:02.000058Z

because instaparse can't keep state of how far to the right you should be at any given point

aengelberg 2018-04-18T23:44:10.000010Z

based on higher-level indentations

2018-04-18T23:44:41.000212Z

I think the combination of writing that code and having to write a grammar is probably more tedious than parsing it manually

aengelberg 2018-04-18T23:44:57.000215Z

perhaps

aengelberg 2018-04-18T23:46:58.000129Z

oh also you could maybe get sneaky with nested parsing

aengelberg 2018-04-18T23:47:59.000008Z

where, say, the top level parser gives you back

[:def "f(x)"
 [:nested-block
  "if x == 1:"
  "  return 2;"
  "else:"
  "  return 3;"]]

aengelberg 2018-04-18T23:48:15.000242Z

and then you have some code that then takes said nested blocks and re-runs the parser on it

aengelberg 2018-04-18T23:48:17.000296Z

and so on

2018-04-18T23:49:01.000218Z

oh interesting

2018-04-18T23:49:22.000103Z

if you're not trampolining your parser, why bother getting up in the morning?

aengelberg 2018-04-18T23:53:41.000048Z

you would have to re-append those nested block lines (with newlines in between)

2018-04-18T23:54:03.000295Z

sure

aengelberg 2018-04-18T23:56:49.000321Z

the grammar would look like

S = def | if | ...
def = <'def '> thing-you're-deffing <':\n'> nested-block
nested-block = (' ' #".*\n?")*