Fork me on GitHub
#instaparse
<
2018-04-18
>
gfredericks23:04:17

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

aengelberg23:04:24

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

gfredericks23:04:43

@aengelberg can you change your username to ængelberg

gfredericks23:04:00

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?

aengelberg23:04:00

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

aengelberg23:04:05

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;
←←

aengelberg23:04:32

if that makes sense

gfredericks23:04:42

and the arrows act like brackets?

aengelberg23:04:02

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

aengelberg23:04:10

based on higher-level indentations

gfredericks23:04:41

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

aengelberg23:04:58

oh also you could maybe get sneaky with nested parsing

aengelberg23:04:59

where, say, the top level parser gives you back

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

aengelberg23:04:15

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

gfredericks23:04:01

oh interesting

gfredericks23:04:22

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

aengelberg23:04:41

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

aengelberg23:04:49

the grammar would look like

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