@@ -34,16 +34,15 @@ use swc_core::{
34
34
FileName , GLOBALS , Mark , SourceFile , SourceMap ,
35
35
comments:: { Comments , SingleThreadedComments } ,
36
36
errors:: Handler ,
37
+ input:: SourceFileInput ,
37
38
} ,
38
39
ecma:: {
39
- ast:: { EsVersion , Pass , Program } ,
40
- parser:: {
41
- Syntax , parse_file_as_commonjs, parse_file_as_module, parse_file_as_program,
42
- parse_file_as_script,
43
- } ,
40
+ ast:: { EsVersion , Module , Pass , Program , Script } ,
41
+ parser:: { Lexer , PResult , Parser , Syntax , unstable:: Capturing } ,
44
42
transforms:: base:: helpers:: { self , Helpers } ,
45
43
} ,
46
44
} ;
45
+ use swc_ecma_lexer:: error:: Error ;
47
46
use swc_error_reporters:: handler:: try_with_handler;
48
47
use url:: Url ;
49
48
@@ -650,3 +649,68 @@ impl<'a> JavaScriptTransformer<'a> {
650
649
}
651
650
}
652
651
}
652
+
653
+ pub fn parse_file_as_module (
654
+ fm : & SourceFile ,
655
+ syntax : Syntax ,
656
+ target : EsVersion ,
657
+ comments : Option < & dyn Comments > ,
658
+ recovered_errors : & mut Vec < Error > ,
659
+ ) -> PResult < Module > {
660
+ with_file_parser ( fm, syntax, target, comments, recovered_errors, |p| {
661
+ p. parse_module ( )
662
+ } )
663
+ }
664
+
665
+ pub fn with_file_parser < T > (
666
+ fm : & SourceFile ,
667
+ syntax : Syntax ,
668
+ target : EsVersion ,
669
+ comments : Option < & dyn Comments > ,
670
+ recovered_errors : & mut Vec < Error > ,
671
+ op : impl for < ' aa > FnOnce ( & mut Parser < Capturing < Lexer > > ) -> PResult < T > ,
672
+ ) -> PResult < T > {
673
+ let lexer = self :: Lexer :: new ( syntax, target, SourceFileInput :: from ( fm) , comments) ;
674
+ let mut p = Parser :: new_from ( Capturing :: new ( lexer) ) ;
675
+ let ret = op ( & mut p) ;
676
+
677
+ recovered_errors. append ( & mut p. take_errors ( ) ) ;
678
+
679
+ ret
680
+ }
681
+
682
+ pub fn parse_file_as_script (
683
+ fm : & SourceFile ,
684
+ syntax : Syntax ,
685
+ target : EsVersion ,
686
+ comments : Option < & dyn Comments > ,
687
+ recovered_errors : & mut Vec < Error > ,
688
+ ) -> PResult < Script > {
689
+ with_file_parser ( fm, syntax, target, comments, recovered_errors, |p| {
690
+ p. parse_script ( )
691
+ } )
692
+ }
693
+
694
+ pub fn parse_file_as_commonjs (
695
+ fm : & SourceFile ,
696
+ syntax : Syntax ,
697
+ target : EsVersion ,
698
+ comments : Option < & dyn Comments > ,
699
+ recovered_errors : & mut Vec < Error > ,
700
+ ) -> PResult < Script > {
701
+ with_file_parser ( fm, syntax, target, comments, recovered_errors, |p| {
702
+ p. parse_commonjs ( )
703
+ } )
704
+ }
705
+
706
+ pub fn parse_file_as_program (
707
+ fm : & SourceFile ,
708
+ syntax : Syntax ,
709
+ target : EsVersion ,
710
+ comments : Option < & dyn Comments > ,
711
+ recovered_errors : & mut Vec < Error > ,
712
+ ) -> PResult < Program > {
713
+ with_file_parser ( fm, syntax, target, comments, recovered_errors, |p| {
714
+ p. parse_program ( )
715
+ } )
716
+ }
0 commit comments