Skip to content

Commit b09329b

Browse files
sasha-gilSasha Gil
andauthored
Refactor parser.rs so it uses RootState::parse; remove module cache logic from shared.rs. (#169)
* Copy more parser.rs logic to shared.rs - cleaning up and properly renaming by the way. * Move code in shared.rs * Factor out parser\anystate.rs from parser\shared.rs * Replace AnyResultWithImportPath with a pair tuple - that allowed to shorten any_states.rs by 33 lines. * Replace JsonStateWithImportPath with a pair tuple, shaving off few lines of code from shared.rs. * Factor const_state.rs, json_state.rs, root_state.rs out of shared.rs. * Correct comments for result pairs in two places (any_state.rs, root_state.rs) * cargo fmt * Push module cache processing from shared.rs to parser.rs; use root_state.parse in root_state_parse. --------- Co-authored-by: Sasha Gil <alexgil@microsoft.com>
1 parent ae98d40 commit b09329b

File tree

5 files changed

+104
-269
lines changed

5 files changed

+104
-269
lines changed

nanvm-lib/src/parser/any_state.rs

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use super::{
22
json_state::JsonState,
3-
path::{concat, split},
43
shared::{
5-
to_js_string, DataType, JsonElement, JsonStackElement, JsonStackObject, ModuleCache,
6-
ParseError, ParseResult, ParsingStatus,
4+
to_js_string, DataType, JsonElement, JsonStackElement, JsonStackObject, ParseError,
5+
ParseResult, ParsingStatus,
76
},
87
};
98
use crate::{
@@ -72,11 +71,9 @@ impl<M: Manager> AnyState<M> {
7271
self,
7372
manager: M,
7473
token: JsonToken,
75-
module_cache: &mut ModuleCache<M::Dealloc>,
76-
context_path: String,
7774
) -> (
7875
/*any_result:*/ AnyResult<M>,
79-
/*import_path:*/ Option<String>,
76+
/*module_name:*/ Option<String>,
8077
) {
8178
match self.status {
8279
ParsingStatus::Initial | ParsingStatus::ObjectColon => {
@@ -90,9 +87,7 @@ impl<M: Manager> AnyState<M> {
9087
ParsingStatus::ObjectValue => (self.parse_object_next(manager, token), None),
9188
ParsingStatus::ObjectComma => (self.parse_object_comma(manager, token), None),
9289
ParsingStatus::ImportBegin => (self.parse_import_begin(token), None),
93-
ParsingStatus::ImportValue => {
94-
self.parse_import_value(token, module_cache, context_path)
95-
}
90+
ParsingStatus::ImportValue => self.parse_import_value(token),
9691
ParsingStatus::ImportEnd => (self.parse_import_end(token), None),
9792
}
9893
}
@@ -101,17 +96,15 @@ impl<M: Manager> AnyState<M> {
10196
self,
10297
manager: M,
10398
token: JsonToken,
104-
module_cache: &mut ModuleCache<M::Dealloc>,
105-
context_path: String,
10699
) -> (
107100
/*json_state:*/ JsonState<M>,
108-
/*import_path:*/ Option<String>,
101+
/*module_name:*/ Option<String>,
109102
) {
110-
let (any_result, import_path) = self.parse(manager, token, module_cache, context_path);
111-
match import_path {
112-
Some(import_path) => {
103+
let (any_result, module_name) = self.parse(manager, token);
104+
match module_name {
105+
Some(module_name) => {
113106
if let AnyResult::Continue(state) = any_result {
114-
(JsonState::ParseModule(state), Some(import_path))
107+
(JsonState::ParseModule(state), Some(module_name))
115108
} else {
116109
panic!("Import path should be returned only with Continue result");
117110
}
@@ -150,37 +143,18 @@ impl<M: Manager> AnyState<M> {
150143
fn parse_import_value(
151144
self,
152145
token: JsonToken,
153-
module_cache: &mut ModuleCache<M::Dealloc>,
154-
context_path: String,
155146
) -> (
156-
AnyResult<M>, /*any_result*/
157-
Option<String>, /*import_path*/
147+
/*any_result:*/ AnyResult<M>,
148+
/*module_name:*/ Option<String>,
158149
) {
159150
match token {
160-
JsonToken::String(s) => {
161-
let import_path = concat(split(&context_path).0, s.as_str());
162-
if let Some(any) = module_cache.complete.get(&import_path) {
163-
return (
164-
AnyResult::Continue(AnyState {
165-
status: ParsingStatus::ImportEnd,
166-
current: JsonElement::Any(any.clone()),
167-
..self
168-
}),
169-
None,
170-
);
171-
}
172-
if module_cache.progress.contains(&import_path) {
173-
return (AnyResult::Error(ParseError::CircularDependency), None);
174-
}
175-
module_cache.progress.insert(import_path.clone());
176-
(
177-
AnyResult::Continue(AnyState {
178-
status: ParsingStatus::ImportEnd,
179-
..self
180-
}),
181-
Some(import_path),
182-
)
183-
}
151+
JsonToken::String(s) => (
152+
AnyResult::Continue(AnyState {
153+
status: ParsingStatus::ImportEnd,
154+
..self
155+
}),
156+
Some(s),
157+
),
184158
_ => (AnyResult::Error(ParseError::WrongRequireStatement), None),
185159
}
186160
}

nanvm-lib/src/parser/const_state.rs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::{
22
any_state::{AnyResult, AnyState},
33
json_state::JsonState,
44
root_state::{RootState, RootStatus},
5-
shared::ModuleCache,
65
};
76
use crate::{mem::manager::Manager, tokenizer::JsonToken};
87

@@ -12,19 +11,13 @@ pub struct ConstState<M: Manager> {
1211
}
1312

1413
impl<M: Manager> ConstState<M> {
15-
pub fn parse(
16-
self,
17-
manager: M,
18-
token: JsonToken,
19-
module_cache: &mut ModuleCache<M::Dealloc>,
20-
context_path: String,
21-
) -> JsonState<M> {
14+
pub fn parse(self, manager: M, token: JsonToken) -> JsonState<M> {
2215
match token {
2316
JsonToken::Semicolon => todo!(),
2417
_ => {
2518
// TODO: use import_path in place of _ below to track possible errors -
2619
// or provide an explanation on why it's not necessary.
27-
let (any_result, _) = self.state.parse(manager, token, module_cache, context_path);
20+
let (any_result, _) = self.state.parse(manager, token);
2821
match any_result {
2922
AnyResult::Continue(state) => JsonState::ParseConst(ConstState {
3023
key: self.key,

nanvm-lib/src/parser/json_state.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use super::{
22
any_state::AnyState,
33
const_state::ConstState,
44
root_state::RootState,
5-
shared::{ModuleCache, ParseError, ParseResult},
5+
shared::{ParseError, ParseResult},
66
};
77
use crate::{mem::manager::Manager, tokenizer::JsonToken};
88

@@ -19,29 +19,24 @@ impl<M: Manager> JsonState<M> {
1919
self,
2020
manager: M,
2121
token: JsonToken,
22-
module_cache: &mut ModuleCache<M::Dealloc>,
23-
context_path: String,
2422
) -> (
2523
/*json_state:*/ JsonState<M>,
26-
/*import_path:*/ Option<String>,
24+
/*import:*/ Option<(/*id:*/ String, /*module:*/ String)>,
2725
) {
2826
if token == JsonToken::NewLine {
2927
return match self {
30-
JsonState::ParseRoot(state) => {
31-
state.parse(manager, token, module_cache, context_path)
32-
}
28+
JsonState::ParseRoot(state) => state.parse(manager, token),
3329
_ => (self, None),
3430
};
3531
}
3632
match self {
37-
JsonState::ParseRoot(state) => state.parse(manager, token, module_cache, context_path),
38-
JsonState::ParseConst(state) => (
39-
state.parse(manager, token, module_cache, context_path),
40-
None,
41-
),
33+
JsonState::ParseRoot(state) => state.parse(manager, token),
34+
JsonState::ParseConst(state) => (state.parse(manager, token), None),
4235
JsonState::Result(_) => (JsonState::Error(ParseError::UnexpectedToken), None),
4336
JsonState::ParseModule(state) => {
44-
state.parse_for_module(manager, token, module_cache, context_path)
37+
let (json_state, _module_name) = state.parse_for_module(manager, token);
38+
// TODO: figure out id and use _module_name to return Some in place of None below.
39+
(json_state, None)
4540
}
4641
_ => (self, None),
4742
}

0 commit comments

Comments
 (0)