@@ -654,10 +654,7 @@ Parser<ManagedTokenSource>::parse_simple_path ()
654
654
// Parse all other simple path segments
655
655
while (lexer.peek_token ()->get_id () == SCOPE_RESOLUTION)
656
656
{
657
- // Skip scope resolution operator
658
- lexer.skip_token ();
659
-
660
- AST::SimplePathSegment new_segment = parse_simple_path_segment ();
657
+ AST::SimplePathSegment new_segment = parse_simple_path_segment (1 );
661
658
662
659
// Return path as currently constructed if segment in error state.
663
660
if (new_segment.is_error ())
@@ -685,35 +682,36 @@ Parser<ManagedTokenSource>::parse_simple_path ()
685
682
}
686
683
687
684
/* Parses a single SimplePathSegment (does not handle the scope resolution
688
- * operators) */
685
+ * operators)
686
+ * Starts parsing at an offset of base_peek */
689
687
template <typename ManagedTokenSource>
690
688
AST::SimplePathSegment
691
- Parser<ManagedTokenSource>::parse_simple_path_segment ()
689
+ Parser<ManagedTokenSource>::parse_simple_path_segment (int base_peek )
692
690
{
693
691
using namespace Values ;
694
- const_TokenPtr t = lexer.peek_token ();
692
+ const_TokenPtr t = lexer.peek_token (base_peek );
695
693
switch (t->get_id ())
696
694
{
697
695
case IDENTIFIER:
698
- lexer.skip_token ();
696
+ lexer.skip_token (base_peek );
699
697
700
698
return AST::SimplePathSegment (t->get_str (), t->get_locus ());
701
699
case SUPER:
702
- lexer.skip_token ();
700
+ lexer.skip_token (base_peek );
703
701
704
702
return AST::SimplePathSegment (Keywords::SUPER, t->get_locus ());
705
703
case SELF:
706
- lexer.skip_token ();
704
+ lexer.skip_token (base_peek );
707
705
708
706
return AST::SimplePathSegment (Keywords::SELF, t->get_locus ());
709
707
case CRATE:
710
- lexer.skip_token ();
708
+ lexer.skip_token (base_peek );
711
709
712
710
return AST::SimplePathSegment (Keywords::CRATE, t->get_locus ());
713
711
case DOLLAR_SIGN:
714
- if (lexer.peek_token (1 )->get_id () == CRATE)
712
+ if (lexer.peek_token (base_peek + 1 )->get_id () == CRATE)
715
713
{
716
- lexer.skip_token (1 );
714
+ lexer.skip_token (base_peek + 1 );
717
715
718
716
return AST::SimplePathSegment (" $crate" , t->get_locus ());
719
717
}
@@ -2807,58 +2805,10 @@ Parser<ManagedTokenSource>::parse_use_tree ()
2807
2805
}
2808
2806
else
2809
2807
{
2810
- /* Due to aforementioned implementation issues, the trailing :: token is
2811
- * consumed by the path, so it can not be used as a disambiguator.
2812
- * NOPE, not true anymore - TODO what are the consequences of this? */
2813
-
2814
2808
const_TokenPtr t = lexer.peek_token ();
2809
+
2815
2810
switch (t->get_id ())
2816
2811
{
2817
- case ASTERISK:
2818
- // glob UseTree type
2819
- lexer.skip_token ();
2820
-
2821
- return std::unique_ptr<AST::UseTreeGlob> (
2822
- new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
2823
- std::move (path), locus));
2824
- case LEFT_CURLY:
2825
- {
2826
- // nested tree UseTree type
2827
- lexer.skip_token ();
2828
-
2829
- std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2830
-
2831
- // TODO: think of better control structure
2832
- const_TokenPtr t = lexer.peek_token ();
2833
- while (t->get_id () != RIGHT_CURLY)
2834
- {
2835
- std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2836
- if (use_tree == nullptr )
2837
- {
2838
- break ;
2839
- }
2840
-
2841
- use_trees.push_back (std::move (use_tree));
2842
-
2843
- if (lexer.peek_token ()->get_id () != COMMA)
2844
- break ;
2845
-
2846
- lexer.skip_token ();
2847
- t = lexer.peek_token ();
2848
- }
2849
-
2850
- // skip end curly delimiter
2851
- if (!skip_token (RIGHT_CURLY))
2852
- {
2853
- // skip after somewhere?
2854
- return nullptr ;
2855
- }
2856
-
2857
- return std::unique_ptr<AST::UseTreeList> (
2858
- new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
2859
- std::move (path), std::move (use_trees),
2860
- locus));
2861
- }
2862
2812
case AS:
2863
2813
{
2864
2814
// rebind UseTree type
@@ -2899,16 +2849,72 @@ Parser<ManagedTokenSource>::parse_use_tree ()
2899
2849
2900
2850
// don't skip semicolon - handled in parse_use_tree
2901
2851
// lexer.skip_token();
2902
-
2903
- return std::unique_ptr<AST::UseTreeRebind> (
2904
- new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
2905
- locus));
2906
2852
case COMMA:
2907
2853
case RIGHT_CURLY:
2908
2854
// this may occur in recursive calls - assume it is ok and ignore it
2909
2855
return std::unique_ptr<AST::UseTreeRebind> (
2910
2856
new AST::UseTreeRebind (AST::UseTreeRebind::NONE, std::move (path),
2911
2857
locus));
2858
+ case SCOPE_RESOLUTION:
2859
+ // keep going
2860
+ break ;
2861
+ default :
2862
+ add_error (Error (t->get_locus (),
2863
+ " unexpected token %qs in use tree with valid path" ,
2864
+ t->get_token_description ()));
2865
+ return nullptr ;
2866
+ }
2867
+
2868
+ skip_token ();
2869
+ t = lexer.peek_token ();
2870
+
2871
+ switch (t->get_id ())
2872
+ {
2873
+ case ASTERISK:
2874
+ // glob UseTree type
2875
+ lexer.skip_token ();
2876
+
2877
+ return std::unique_ptr<AST::UseTreeGlob> (
2878
+ new AST::UseTreeGlob (AST::UseTreeGlob::PATH_PREFIXED,
2879
+ std::move (path), locus));
2880
+ case LEFT_CURLY:
2881
+ {
2882
+ // nested tree UseTree type
2883
+ lexer.skip_token ();
2884
+
2885
+ std::vector<std::unique_ptr<AST::UseTree>> use_trees;
2886
+
2887
+ // TODO: think of better control structure
2888
+ const_TokenPtr t = lexer.peek_token ();
2889
+ while (t->get_id () != RIGHT_CURLY)
2890
+ {
2891
+ std::unique_ptr<AST::UseTree> use_tree = parse_use_tree ();
2892
+ if (use_tree == nullptr )
2893
+ {
2894
+ break ;
2895
+ }
2896
+
2897
+ use_trees.push_back (std::move (use_tree));
2898
+
2899
+ if (lexer.peek_token ()->get_id () != COMMA)
2900
+ break ;
2901
+
2902
+ lexer.skip_token ();
2903
+ t = lexer.peek_token ();
2904
+ }
2905
+
2906
+ // skip end curly delimiter
2907
+ if (!skip_token (RIGHT_CURLY))
2908
+ {
2909
+ // skip after somewhere?
2910
+ return nullptr ;
2911
+ }
2912
+
2913
+ return std::unique_ptr<AST::UseTreeList> (
2914
+ new AST::UseTreeList (AST::UseTreeList::PATH_PREFIXED,
2915
+ std::move (path), std::move (use_trees),
2916
+ locus));
2917
+ }
2912
2918
default :
2913
2919
add_error (Error (t->get_locus (),
2914
2920
" unexpected token %qs in use tree with valid path" ,
0 commit comments