File tree Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Expand file tree Collapse file tree 2 files changed +45
-5
lines changed Original file line number Diff line number Diff line change 1
1
use crate :: { File , SyntaxKind , SyntaxNodeRef , WalkEvent } ;
2
2
use std:: fmt:: Write ;
3
+ use std:: ops:: Deref ;
4
+ use std:: str;
3
5
4
6
/// Parse a file and create a string representation of the resulting parse tree.
5
7
pub fn dump_tree ( syntax : SyntaxNodeRef ) -> String {
@@ -78,3 +80,38 @@ pub(crate) fn validate_block_structure(root: SyntaxNodeRef) {
78
80
}
79
81
}
80
82
}
83
+
84
+ #[ derive( Debug ) ]
85
+ pub struct MutAsciiString < ' a > {
86
+ buf : & ' a mut [ u8 ] ,
87
+ len : usize ,
88
+ }
89
+
90
+ impl < ' a > MutAsciiString < ' a > {
91
+ pub fn new ( buf : & ' a mut [ u8 ] ) -> MutAsciiString < ' a > {
92
+ MutAsciiString { buf, len : 0 }
93
+ }
94
+
95
+ pub fn as_str ( & self ) -> & str {
96
+ str:: from_utf8 ( & self . buf [ ..self . len ] ) . unwrap ( )
97
+ }
98
+
99
+ pub fn len ( & self ) -> usize {
100
+ self . len
101
+ }
102
+
103
+ pub fn push ( & mut self , c : char ) {
104
+ assert ! ( self . len( ) < self . buf. len( ) ) ;
105
+ assert ! ( c. is_ascii( ) ) ;
106
+
107
+ self . buf [ self . len ] = c as u8 ;
108
+ self . len += 1 ;
109
+ }
110
+ }
111
+
112
+ impl < ' a > Deref for MutAsciiString < ' a > {
113
+ type Target = str ;
114
+ fn deref ( & self ) -> & str {
115
+ self . as_str ( )
116
+ }
117
+ }
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ use crate::{
5
5
ast:: { self , AstNode } ,
6
6
File ,
7
7
string_lexing:: { self , CharComponentKind } ,
8
+ utils:: MutAsciiString ,
8
9
yellow:: {
9
10
SyntaxError ,
10
11
SyntaxErrorKind :: * ,
@@ -73,12 +74,18 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
73
74
return ;
74
75
}
75
76
76
- let mut code = String :: new ( ) ;
77
+ let mut buf = & mut [ 0 ; 6 ] ;
78
+ let mut code = MutAsciiString :: new ( buf) ;
77
79
let mut closed = false ;
78
80
for c in text[ 3 ..] . chars ( ) {
79
81
assert ! ( !closed, "no characters after escape is closed" ) ;
80
82
81
83
if c. is_digit ( 16 ) {
84
+ if code. len ( ) == 6 {
85
+ errors. push ( SyntaxError :: new ( OverlongUnicodeEscape , range) ) ;
86
+ return ;
87
+ }
88
+
82
89
code. push ( c) ;
83
90
} else if c == '_' {
84
91
// Reject leading _
@@ -103,10 +110,6 @@ fn validate_char(node: ast::Char, errors: &mut Vec<SyntaxError>) {
103
110
return ;
104
111
}
105
112
106
- if code. len ( ) > 6 {
107
- errors. push ( SyntaxError :: new ( OverlongUnicodeEscape , range) ) ;
108
- }
109
-
110
113
match u32:: from_str_radix ( & code, 16 ) {
111
114
Ok ( code_u32) if code_u32 > 0x10FFFF => {
112
115
errors. push ( SyntaxError :: new ( UnicodeEscapeOutOfRange , range) ) ;
You can’t perform that action at this time.
0 commit comments