@@ -2,22 +2,25 @@ import {ExternalTokenizer, ContextTracker} from "@lezer/lr"
2
2
import {
3
3
newline as newlineToken , eof , newlineEmpty , newlineBracketed , indent , dedent , printKeyword ,
4
4
ParenthesizedExpression , TupleExpression , ComprehensionExpression ,
5
- ArrayExpression , ArrayComprehensionExpression , ArgList , ParamList ,
6
- DictionaryExpression , DictionaryComprehensionExpression , SetExpression , SetComprehensionExpression
5
+ ArrayExpression , ArrayComprehensionExpression , ArgList , ParamList , importList , subscript ,
6
+ DictionaryExpression , DictionaryComprehensionExpression , SetExpression , SetComprehensionExpression , FormatReplacement ,
7
+ ParenL , BraceL , BracketL
7
8
} from "./parser.terms.js"
8
9
9
10
const newline = 10 , carriageReturn = 13 , space = 32 , tab = 9 , hash = 35 , parenOpen = 40 , dot = 46
10
11
11
12
const bracketed = [
12
- ParenthesizedExpression , TupleExpression , ComprehensionExpression , ArrayExpression , ArrayComprehensionExpression ,
13
- DictionaryExpression , DictionaryComprehensionExpression , SetExpression , SetComprehensionExpression , ArgList , ParamList
13
+ ParenthesizedExpression , TupleExpression , ComprehensionExpression , importList , ArgList , ParamList ,
14
+ ArrayExpression , ArrayComprehensionExpression , subscript ,
15
+ SetExpression , SetComprehensionExpression ,
16
+ DictionaryExpression , DictionaryComprehensionExpression , FormatReplacement
14
17
]
15
18
16
19
export const newlines = new ExternalTokenizer ( ( input , stack ) => {
17
20
if ( input . next < 0 ) {
18
21
input . acceptToken ( eof )
19
22
} else if ( input . next != newline && input . next != carriageReturn ) {
20
- } else if ( stack . startOf ( bracketed ) != null ) {
23
+ } else if ( stack . context . depth < 0 ) {
21
24
input . acceptToken ( newlineBracketed , 1 )
22
25
} else {
23
26
input . advance ( )
@@ -29,8 +32,10 @@ export const newlines = new ExternalTokenizer((input, stack) => {
29
32
} , { contextual : true , fallback : true } )
30
33
31
34
export const indentation = new ExternalTokenizer ( ( input , stack ) => {
35
+ let cDepth = stack . context . depth
36
+ if ( cDepth < 0 ) return
32
37
let prev = input . peek ( - 1 ) , depth
33
- if ( ( prev == newline || prev == carriageReturn ) && stack . startOf ( bracketed ) == null ) {
38
+ if ( ( prev == newline || prev == carriageReturn ) && stack . context . depth >= 0 ) {
34
39
let depth = 0 , chars = 0
35
40
for ( ; ; ) {
36
41
if ( input . next == space ) depth ++
@@ -39,16 +44,17 @@ export const indentation = new ExternalTokenizer((input, stack) => {
39
44
input . advance ( )
40
45
chars ++
41
46
}
42
- if ( depth != stack . context . depth &&
47
+ if ( depth != cDepth &&
43
48
input . next != newline && input . next != carriageReturn && input . next != hash ) {
44
- if ( depth < stack . context . depth ) input . acceptToken ( dedent , - chars )
49
+ if ( depth < cDepth ) input . acceptToken ( dedent , - chars )
45
50
else input . acceptToken ( indent )
46
51
}
47
52
}
48
53
} )
49
54
50
55
function IndentLevel ( parent , depth ) {
51
56
this . parent = parent
57
+ // -1 means this is not an actual indent level but a set of brackets
52
58
this . depth = depth
53
59
this . hash = ( parent ? parent . hash + parent . hash << 8 : 0 ) + depth + ( depth << 4 )
54
60
}
@@ -57,8 +63,14 @@ const topIndent = new IndentLevel(null, 0)
57
63
58
64
export const trackIndent = new ContextTracker ( {
59
65
start : topIndent ,
66
+ reduce ( context , term ) {
67
+ return context . depth < 0 && bracketed . indexOf ( term ) > - 1 ? context . parent : context
68
+ } ,
60
69
shift ( context , term , stack , input ) {
61
- return term == indent ? new IndentLevel ( context , stack . pos - input . pos ) : term == dedent ? context . parent : context
70
+ if ( term == indent ) return new IndentLevel ( context , stack . pos - input . pos )
71
+ if ( term == dedent ) return context . parent
72
+ if ( term == ParenL || term == BracketL || term == BraceL ) return new IndentLevel ( context , - 1 )
73
+ return context
62
74
} ,
63
75
hash ( context ) { return context . hash }
64
76
} )
0 commit comments