|
19 | 19 | NL = OneOrMore(LineEnd().setWhitespaceChars("\t ").suppress()).setName("Newline") |
20 | 20 |
|
21 | 21 |
|
22 | | -def customIndentedBlock( |
23 | | - blockStatementExpr, indentStack, indent=True, terminal=False, lax=False |
24 | | -): |
25 | | - """ |
26 | | - Modified version of the indentedBlock construct provided by pyparsing. Allows fuzzier indent boundaries |
27 | | - """ |
28 | | - backup_stack = indentStack[:] |
29 | | - |
30 | | - def reset_stack(): |
31 | | - indentStack[:] = backup_stack |
32 | | - |
33 | | - def checkPeerIndent(s, l, t): |
34 | | - if l >= len(s): |
35 | | - return |
36 | | - curCol = col(l, s) |
37 | | - |
38 | | - # A terminal block doesn't have children, so we can assume that any sub-indent is a peer |
39 | | - if terminal and curCol >= indentStack[-1]: |
40 | | - return |
41 | | - |
42 | | - # If we're being lax, anything that's not a full dedent is a peer |
43 | | - if lax and curCol > indentStack[-2]: |
44 | | - return |
45 | | - |
46 | | - # Anything that is indented more than the previous indent level counts as a peer |
47 | | - if curCol < indentStack[-1] or curCol <= indentStack[-2]: |
48 | | - raise ParseException(s, l, "not a peer entry") |
49 | | - |
50 | | - def checkSubIndent(s, l, t): |
51 | | - curCol = col(l, s) |
52 | | - if curCol > indentStack[-1]: |
53 | | - indentStack.append(curCol) |
54 | | - else: |
55 | | - raise ParseException(s, l, "not a subentry") |
56 | | - |
57 | | - def checkUnindent(s, l, t): |
58 | | - if l >= len(s): |
59 | | - indentStack.pop() |
60 | | - return |
61 | | - curCol = col(l, s) |
62 | | - if not (indentStack and curCol < indentStack[-1]): |
63 | | - raise ParseException(s, l, "not an unindent") |
64 | | - indentStack.pop() |
65 | | - |
66 | | - INDENT = (Empty() + Empty().setParseAction(checkSubIndent)).setName("Indent") |
67 | | - PEER = Empty().setParseAction(checkPeerIndent).setName("PeerIndent") |
68 | | - UNDENT = Empty().setParseAction(checkUnindent).setName("Unindent") |
69 | | - if indent: |
70 | | - smExpr = Group( |
71 | | - Optional(NL) |
72 | | - + |
73 | | - # ~ FollowedBy(blockStatementExpr) + |
74 | | - INDENT |
75 | | - + (OneOrMore(PEER + Group(blockStatementExpr) + Optional(NL))) |
76 | | - + UNDENT |
77 | | - ) |
78 | | - else: |
79 | | - smExpr = Group( |
80 | | - Optional(NL) + (OneOrMore(PEER + Group(blockStatementExpr) + Optional(NL))) |
81 | | - ) |
82 | | - smExpr.setFailAction(lambda a, b, c, d: reset_stack()) |
83 | | - blockStatementExpr.ignore("\\" + LineEnd()) |
84 | | - return smExpr.setName("IndentedBlock") |
85 | | - |
86 | | - |
87 | 22 | cli_id = Word(initChars=element_start_chars, bodyChars=element_body_chars) |
88 | 23 |
|
89 | 24 | positional_name = Word( |
|
0 commit comments