1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
7 indexed_source_text::IndexedSourceText,
10 source_text::SourceText,
11 syntax_by_ref::serialize,
14 positioned_token::{PositionedTokenFullTrivia, TokenFactoryFullTrivia},
15 positioned_value::PositionedValueFullTrivia,
18 syntax_error::SyntaxError,
20 use positioned_smart_constructors::*;
21 use serde::{Serialize, Serializer};
22 use stack_limit::StackLimit;
25 syntax::Syntax<'a, PositionedTokenFullTrivia<'a>, PositionedValueFullTrivia<'a>>;
27 pub type SmartConstructors<'a> =
28 PositionedSmartConstructors<Syntax<'a>, TokenFactoryFullTrivia<'a>, State<'a>>;
30 pub fn parse_script<'src, 'arena>(
32 source: &SourceText<'src>,
34 stack_limit: Option<&'src StackLimit>,
35 ) -> (Syntax<'arena>, Vec<SyntaxError>) {
36 let tf = TokenFactoryFullTrivia::new(arena);
37 let sc = SmartConstructors::new(State { arena }, tf);
38 let mut parser = Parser::new(&source, env, sc);
39 let root = parser.parse_script(stack_limit);
40 let errors = parser.errors();
44 pub fn parse_script_to_json<'src, 'arena, S: Serializer>(
47 source: &IndexedSourceText<'src>,
49 stack_limit: Option<&'src StackLimit>,
50 ) -> Result<S::Ok, S::Error> {
53 parse_tree: serialize::WithContext<'a, Syntax<'a>>,
54 program_text: &'a str,
55 version: &'static str,
58 let tf = TokenFactoryFullTrivia::new(arena);
59 let sc = SmartConstructors::new(State { arena }, tf);
60 let mut parser = Parser::new(source.source_text(), env, sc);
61 let root = parser.parse_script(stack_limit);
63 let parse_tree = serialize::WithContext(&source, &root);
66 program_text: source.source_text().text_as_str(),
67 version: full_fidelity_schema_version_number::VERSION,