@@ -2,19 +2,24 @@ import {ExternalTokenizer, ContextTracker} from "@lezer/lr"
2
2
import {
3
3
newline as newlineToken , eof , newlineBracketed , blankLineStart , indent , dedent , printKeyword ,
4
4
ParenthesizedExpression , TupleExpression , ComprehensionExpression ,
5
- PatternArgList , SequencePattern , MappingPattern ,
5
+ PatternArgList , SequencePattern , MappingPattern , FormatString ,
6
6
ArrayExpression , ArrayComprehensionExpression , ArgList , ParamList , importList , subscript ,
7
- DictionaryExpression , DictionaryComprehensionExpression , SetExpression , SetComprehensionExpression , FormatReplacement ,
7
+ DictionaryExpression , DictionaryComprehensionExpression , SetExpression , SetComprehensionExpression ,
8
+ formatString1Content , formatString1Brace , formatString1End ,
9
+ formatString2Content , formatString2Brace , formatString2End ,
10
+ formatString1lContent , formatString1lBrace , formatString1lEnd ,
11
+ formatString2lContent , formatString2lBrace , formatString2lEnd ,
8
12
ParenL , BraceL , BracketL
9
13
} from "./parser.terms.js"
10
14
11
- const newline = 10 , carriageReturn = 13 , space = 32 , tab = 9 , hash = 35 , parenOpen = 40 , dot = 46
15
+ const newline = 10 , carriageReturn = 13 , space = 32 , tab = 9 , hash = 35 , parenOpen = 40 , dot = 46 ,
16
+ braceOpen = 123 , singleQuote = 39 , doubleQuote = 34
12
17
13
18
const bracketed = new Set ( [
14
19
ParenthesizedExpression , TupleExpression , ComprehensionExpression , importList , ArgList , ParamList ,
15
20
ArrayExpression , ArrayComprehensionExpression , subscript ,
16
- SetExpression , SetComprehensionExpression ,
17
- DictionaryExpression , DictionaryComprehensionExpression , FormatReplacement ,
21
+ SetExpression , SetComprehensionExpression , FormatString ,
22
+ DictionaryExpression , DictionaryComprehensionExpression ,
18
23
SequencePattern , MappingPattern , PatternArgList
19
24
] )
20
25
@@ -102,3 +107,41 @@ export const legacyPrint = new ExternalTokenizer(input => {
102
107
return
103
108
}
104
109
} )
110
+
111
+ function formatString ( quote , len , content , brace , end ) {
112
+ return new ExternalTokenizer ( input => {
113
+ let start = input . pos
114
+ for ( ; ; ) {
115
+ if ( input . next < 0 ) {
116
+ break
117
+ } else if ( input . next == braceOpen ) {
118
+ if ( input . peek ( 1 ) == braceOpen ) {
119
+ input . advance ( 2 )
120
+ } else {
121
+ if ( input . pos == start ) {
122
+ input . acceptToken ( brace , 1 )
123
+ return
124
+ }
125
+ break
126
+ }
127
+ } else if ( input . next == "\\" ) {
128
+ input . advance ( )
129
+ if ( input . next >= 0 ) input . advance ( )
130
+ } else if ( input . next == quote && ( len == 1 || input . peek ( 1 ) == quote && input . peek ( 2 ) == quote ) ) {
131
+ if ( input . pos == start ) {
132
+ input . acceptToken ( end , len )
133
+ return
134
+ }
135
+ break
136
+ } else {
137
+ input . advance ( )
138
+ }
139
+ }
140
+ if ( input . pos > start ) input . acceptToken ( content )
141
+ } )
142
+ }
143
+
144
+ export const formatString1 = formatString ( singleQuote , 1 , formatString1Content , formatString1Brace , formatString1End )
145
+ export const formatString2 = formatString ( doubleQuote , 1 , formatString2Content , formatString2Brace , formatString2End )
146
+ export const formatString1l = formatString ( singleQuote , 3 , formatString1lContent , formatString1lBrace , formatString1lEnd )
147
+ export const formatString2l = formatString ( doubleQuote , 3 , formatString2lContent , formatString2lBrace , formatString2lEnd )
0 commit comments