go_parser/
lib.rs

1// Copyright 2022 The Goscript Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5//! This crate is part of the Goscript project. Please refer to <https://goscript.dev> for more information.
6//!
7//! It's a port of the the parser from the Go standard library <https://github.com/golang/go/tree/release-branch.go1.12/src/go/parser>
8//!  
9//! # Usage:
10//! ```
11//! fn parse_file() {
12//!     let source = "package main ...";
13//!     let mut fs = go_parser::FileSet::new();
14//!     let o = &mut go_parser::AstObjects::new();
15//!     let el = &mut go_parser::ErrorList::new();
16//!     let (p, _) = go_parser::parse_file(o, &mut fs, el, "./main.go", source, false);
17//!     print!("{}", p.get_errors());
18//! }
19//! ```
20//!
21//! # Feature
22//! - `btree_map`: Make it use BTreeMap instead of HashMap
23//!
24
25mod errors;
26mod map;
27mod objects;
28mod parser;
29mod position;
30mod scanner;
31mod token;
32
33pub mod ast;
34pub mod scope;
35pub mod visitor;
36
37pub use errors::*;
38pub use map::{Map, MapIter};
39pub use objects::*;
40pub use parser::Parser;
41pub use position::*;
42pub use token::*;
43
44pub fn parse_file<'a>(
45    o: &'a mut AstObjects,
46    fs: &'a mut FileSet,
47    el: &'a ErrorList,
48    name: &str,
49    src: &'a str,
50    trace: bool,
51) -> (parser::Parser<'a>, Option<ast::File>) {
52    let f = fs.add_file(name.to_string(), None, src.chars().count());
53    let mut p = parser::Parser::new(o, f, el, src, trace);
54    let file = p.parse_file();
55    (p, file)
56}