@@ -312,7 +312,7 @@ impl ParseState {
312312 ops. push ( ( index, ScopeStackOp :: Pop ( v. len ( ) ) ) ) ;
313313 }
314314
315- if initial && cur_context. clear_scopes != None {
315+ if ! initial && cur_context. clear_scopes != None {
316316 ops. push ( ( index, ScopeStackOp :: Restore ) ) ;
317317 }
318318 }
@@ -483,4 +483,96 @@ mod tests {
483483
484484 // assert!(false);
485485 }
486+
487+ fn expect_scope_stacks ( line : & str , expect : & [ & str ] ) {
488+ // check that each expected scope stack appears at least once while parsing the given test line
489+
490+ //let syntax = SyntaxSet::load_syntax_file("testdata/parser_tests.sublime-syntax", true).unwrap();
491+ use std:: fs:: File ;
492+ use std:: io:: Read ;
493+ let mut f = File :: open ( "testdata/parser_tests.sublime-syntax" ) . unwrap ( ) ;
494+ let mut s = String :: new ( ) ;
495+ f. read_to_string ( & mut s) . unwrap ( ) ;
496+
497+ let syntax = SyntaxDefinition :: load_from_str ( & s, true ) . unwrap ( ) ;
498+
499+ let mut state = ParseState :: new ( & syntax) ;
500+
501+ let mut ss = SyntaxSet :: new ( ) ;
502+ ss. add_syntax ( syntax) ;
503+ ss. link_syntaxes ( ) ;
504+
505+ let mut stack = ScopeStack :: new ( ) ;
506+ let ops = state. parse_line ( line) ;
507+ debug_print_ops ( line, & ops) ;
508+
509+ let mut criteria_met = Vec :: new ( ) ;
510+ for & ( _, ref op) in ops. iter ( ) {
511+ stack. apply ( op) ;
512+ let stack_str = format ! ( "{:?}" , stack) ;
513+ println ! ( "{}" , stack_str) ;
514+ for expectation in expect. iter ( ) {
515+ if stack_str. contains ( expectation) {
516+ criteria_met. push ( expectation) ;
517+ }
518+ }
519+ }
520+ if let Some ( missing) = expect. iter ( ) . filter ( |e| !criteria_met. contains ( & e) ) . next ( ) {
521+ panic ! ( "expected scope stack '{}' missing" , missing) ;
522+ }
523+ }
524+
525+ #[ test]
526+ fn can_parse_non_nested_clear_scopes ( ) {
527+ let line = "'hello #simple_cleared_scopes_test world test \\ n '\n " ;
528+ let expect = [
529+ "<source.test>, <example.meta-scope.after-clear-scopes.example>, <example.pushes-clear-scopes.example>" ,
530+ "<source.test>, <example.meta-scope.after-clear-scopes.example>, <example.pops-clear-scopes.example>" ,
531+ "<source.test>, <string.quoted.single.example>, <constant.character.escape.example>" ,
532+ ] ;
533+ expect_scope_stacks ( & line, & expect) ;
534+ }
535+
536+ #[ test]
537+ fn can_parse_non_nested_too_many_clear_scopes ( ) {
538+ let line = "'hello #too_many_cleared_scopes_test world test \\ n '\n " ;
539+ let expect = [
540+ "<example.meta-scope.after-clear-scopes.example>, <example.pushes-clear-scopes.example>" ,
541+ "<example.meta-scope.after-clear-scopes.example>, <example.pops-clear-scopes.example>" ,
542+ "<source.test>, <string.quoted.single.example>, <constant.character.escape.example>" ,
543+ ] ;
544+ expect_scope_stacks ( & line, & expect) ;
545+ }
546+
547+ #[ test]
548+ fn can_parse_nested_clear_scopes ( ) {
549+ let line = "'hello #nested_clear_scopes_test world foo bar test \\ n '\n " ;
550+ let expect = [
551+ "<source.test>, <example.meta-scope.after-clear-scopes.example>, <example.pushes-clear-scopes.example>" ,
552+ "<source.test>, <example.meta-scope.cleared-previous-meta-scope.example>, <foo>" ,
553+ "<source.test>, <example.meta-scope.after-clear-scopes.example>, <example.pops-clear-scopes.example>" ,
554+ "<source.test>, <string.quoted.single.example>, <constant.character.escape.example>" ,
555+ ] ;
556+ expect_scope_stacks ( & line, & expect) ;
557+ }
558+
559+ #[ test]
560+ fn can_parse_infinite_loop ( ) {
561+ let line = "#infinite_loop_test 123\n " ;
562+ let expect = [
563+ "<source.test>, <constant.numeric.test>" ,
564+ ] ;
565+ expect_scope_stacks ( & line, & expect) ;
566+ }
567+
568+ #[ test]
569+ fn can_parse_infinite_seeming_loop ( ) {
570+ let line = "#infinite_seeming_loop_test hello\n " ;
571+ let expect = [
572+ "<source.test>, <keyword.test>" ,
573+ "<source.test>, <test>, <string.unquoted.test>" ,
574+ "<source.test>, <test>, <keyword.control.test>" ,
575+ ] ;
576+ expect_scope_stacks ( & line, & expect) ;
577+ }
486578}
0 commit comments