@@ -4,7 +4,7 @@ use crate::ast::{
44} ;
55use crate :: tagspecs:: TagSpec ;
66use crate :: tokens:: { Token , TokenStream , TokenType } ;
7- use std:: collections:: { BTreeMap , HashMap } ;
7+ use std:: collections:: BTreeMap ;
88use thiserror:: Error ;
99
1010pub struct Parser {
@@ -27,19 +27,19 @@ impl Parser {
2727 ast. add_node ( node) ;
2828 had_nodes = true ;
2929 }
30- Err ( ParserError :: StreamError { kind } ) if kind == "AtEnd" . to_string ( ) => {
30+ Err ( ParserError :: StreamError { kind } ) if kind == * "AtEnd" => {
3131 if !had_nodes {
3232 return Err ( ParserError :: stream_error ( "UnexpectedEof" ) ) ;
3333 }
3434 break ;
3535 }
36- Err ( ParserError :: ErrorSignal ( Signal :: SpecialTag ( tag ) ) ) => {
36+ Err ( ParserError :: ErrorSignal ( Signal :: SpecialTag ( _ ) ) ) => {
3737 continue ;
3838 }
3939 Err ( ParserError :: UnclosedTag ( tag) ) => {
4040 return Err ( ParserError :: UnclosedTag ( tag) ) ;
4141 }
42- Err ( e ) => {
42+ Err ( _ ) => {
4343 self . synchronize ( ) ?;
4444 continue ;
4545 }
@@ -155,23 +155,18 @@ impl Parser {
155155
156156 let specs = TagSpec :: load_builtin_specs ( ) . unwrap_or_default ( ) ;
157157
158- // Check if this is a closing tag according to ANY spec
158+ // Check if closing or intermediate tag according to any spec
159159 for ( _, spec) in specs. iter ( ) {
160160 if Some ( & tag_name) == spec. closing . as_ref ( ) {
161161 return Err ( ParserError :: ErrorSignal ( Signal :: SpecialTag ( tag_name) ) ) ;
162162 }
163- }
164-
165- // Check if this is an intermediate tag according to ANY spec
166- for ( _, spec) in specs. iter ( ) {
167163 if let Some ( intermediates) = & spec. intermediates {
168164 if intermediates. contains ( & tag_name) {
169165 return Err ( ParserError :: ErrorSignal ( Signal :: SpecialTag ( tag_name) ) ) ;
170166 }
171167 }
172168 }
173169
174- // Get the tag spec for this tag
175170 let tag_spec = specs. get ( tag_name. as_str ( ) ) . cloned ( ) ;
176171
177172 let mut children = Vec :: new ( ) ;
@@ -184,9 +179,8 @@ impl Parser {
184179 }
185180 Err ( ParserError :: ErrorSignal ( Signal :: SpecialTag ( tag) ) ) => {
186181 if let Some ( spec) = & tag_spec {
187- // Check if this is a closing tag
182+ // Check if closing tag
188183 if Some ( & tag) == spec. closing . as_ref ( ) {
189- // Found our closing tag, create appropriate tag type
190184 let tag_node = if !branches. is_empty ( ) {
191185 TagNode :: Branching {
192186 name : tag_name,
@@ -203,10 +197,9 @@ impl Parser {
203197 } ;
204198 return Ok ( Node :: Django ( DjangoNode :: Tag ( tag_node) ) ) ;
205199 }
206- // Check if this is an intermediate tag
200+ // Check if intermediate tag
207201 if let Some ( intermediates) = & spec. intermediates {
208202 if intermediates. contains ( & tag) {
209- // Add current children as a branch and start fresh
210203 branches. push ( TagNode :: Block {
211204 name : tag. clone ( ) ,
212205 bits : vec ! [ tag. clone( ) ] ,
@@ -217,7 +210,6 @@ impl Parser {
217210 }
218211 }
219212 }
220- // If we get here, it's an unexpected tag
221213 return Err ( ParserError :: UnexpectedTag ( tag) ) ;
222214 }
223215 Err ( e) => {
@@ -226,7 +218,7 @@ impl Parser {
226218 }
227219 }
228220
229- // If we get here, we never found the closing tag
221+ // never found the closing tag
230222 Err ( ParserError :: UnclosedTag ( tag_name) )
231223 }
232224
@@ -443,10 +435,6 @@ impl Parser {
443435 self . peek_at ( -1 )
444436 }
445437
446- fn peek_forward ( & self , steps : usize ) -> Result < Vec < Token > , ParserError > {
447- ( 0 ..steps) . map ( |i| self . peek_at ( i as isize ) ) . collect ( )
448- }
449-
450438 fn peek_back ( & self , steps : usize ) -> Result < Vec < Token > , ParserError > {
451439 ( 1 ..=steps) . map ( |i| self . peek_at ( -( i as isize ) ) ) . collect ( )
452440 }
@@ -493,34 +481,6 @@ impl Parser {
493481 self . peek_next ( )
494482 }
495483
496- fn lookahead ( & self , types : & [ TokenType ] ) -> Result < bool , ParserError > {
497- for ( i, t) in types. iter ( ) . enumerate ( ) {
498- if !self . peek_at ( i as isize ) ?. is_token_type ( t) {
499- return Ok ( false ) ;
500- }
501- }
502- Ok ( true )
503- }
504-
505- fn consume_if ( & mut self , token_type : TokenType ) -> Result < Token , ParserError > {
506- let token = self . consume ( ) ?;
507- if token. token_type ( ) == & token_type {
508- Ok ( token)
509- } else {
510- self . backtrack ( 1 ) ?;
511- Err ( ParserError :: token_error ( format ! ( "{:?}" , token_type) , token) )
512- }
513- }
514-
515- fn consume_until ( & mut self , end_type : TokenType ) -> Result < Vec < Token > , ParserError > {
516- let mut consumed = Vec :: new ( ) ;
517- while !self . is_at_end ( ) && self . peek ( ) ?. is_token_type ( & end_type) {
518- let token = self . consume ( ) ?;
519- consumed. push ( token) ;
520- }
521- Ok ( consumed)
522- }
523-
524484 fn synchronize ( & mut self ) -> Result < ( ) , ParserError > {
525485 const SYNC_TYPES : & [ TokenType ] = & [
526486 TokenType :: DjangoBlock ( String :: new ( ) ) ,
@@ -536,7 +496,7 @@ impl Parser {
536496 let current = self . peek ( ) ?;
537497
538498 for sync_type in SYNC_TYPES {
539- if matches ! ( current. token_type( ) , sync_type) {
499+ if current. token_type ( ) == sync_type {
540500 return Ok ( ( ) ) ;
541501 }
542502 }
0 commit comments