1
1
mod utils;
2
2
3
- use darklua_core:: {
4
- generator:: { LuaGenerator , TokenBasedLuaGenerator } ,
5
- nodes:: Block ,
6
- rules:: { self , get_default_rules, Context , Rule } ,
7
- Parser ,
8
- } ;
9
- use serde:: { Deserialize , Serialize } ;
3
+ use darklua_core:: { Configuration , Options , Resources } ;
10
4
use utils:: set_panic_hook;
11
5
use wasm_bindgen:: { prelude:: wasm_bindgen, JsValue } ;
12
6
@@ -16,26 +10,12 @@ use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
16
10
#[ global_allocator]
17
11
static ALLOC : wee_alloc:: WeeAlloc = wee_alloc:: WeeAlloc :: INIT ;
18
12
19
- #[ derive( Serialize , Deserialize ) ]
20
- pub struct Config {
21
- #[ serde( default = "get_default_rules" ) ]
22
- pub rules : Vec < Box < dyn Rule > > ,
23
- }
24
-
25
- fn generate_code ( original_code : & str , block : & Block ) -> String {
26
- let mut generator = TokenBasedLuaGenerator :: new ( original_code) ;
27
- generator. write_block ( block) ;
28
- generator. into_string ( )
29
- }
30
-
31
13
#[ wasm_bindgen]
32
14
pub fn process_code ( code : & str , opt_config : JsValue ) -> Result < String , JsValue > {
33
15
set_panic_hook ( ) ;
34
16
35
17
let config = if opt_config. is_undefined ( ) {
36
- Config {
37
- rules : get_default_rules ( ) ,
38
- }
18
+ Configuration :: default ( )
39
19
} else if opt_config. is_object ( ) {
40
20
let config_string = String :: from ( js_sys:: JSON :: stringify ( & opt_config) ?) ;
41
21
json5:: from_str ( & config_string)
@@ -50,32 +30,34 @@ pub fn process_code(code: &str, opt_config: JsValue) -> Result<String, JsValue>
50
30
} ) ?
51
31
} ;
52
32
53
- let parser = Parser :: default ( ) . preserve_tokens ( ) ;
33
+ let resources = Resources :: from_memory ( ) ;
34
+ const LOCATION : & str = "file.lua" ;
35
+ resources. write ( LOCATION , code) . unwrap ( ) ;
54
36
55
- let mut block = parser. parse ( code) . map_err ( |error| error. to_string ( ) ) ?;
37
+ let result = darklua_core:: process (
38
+ & resources,
39
+ Options :: new ( LOCATION ) . with_configuration ( config) ,
40
+ ) ;
56
41
57
- for ( index, rule) in config. rules . iter ( ) . enumerate ( ) {
58
- let mut context = Context :: default ( ) ;
59
- rule. process ( & mut block, & mut context)
60
- . map_err ( |rule_errors| {
61
- let errors: Vec < _ > = rule_errors. iter ( ) . map ( ToString :: to_string) . collect ( ) ;
62
- format ! (
63
- "error with rule {} ({}):\n -> {}" ,
64
- rule. get_name( ) . to_owned( ) ,
65
- index,
66
- errors. join( "\n -> " ) ,
67
- )
68
- } ) ?;
69
- }
70
-
71
- let lua_code = generate_code ( & code, & block) ;
42
+ match result. result ( ) {
43
+ Ok ( ( ) ) => {
44
+ let lua_code = resources. get ( LOCATION ) . unwrap ( ) ;
72
45
73
- Ok ( lua_code)
46
+ Ok ( lua_code)
47
+ }
48
+ Err ( errors) => {
49
+ let errors: Vec < _ > = errors
50
+ . into_iter ( )
51
+ . map ( |error| format ! ( "-> {}" , error) )
52
+ . collect ( ) ;
53
+ Err ( format ! ( "unable to process code:\n {}" , errors. join( "\n " ) ) . into ( ) )
54
+ }
55
+ }
74
56
}
75
57
76
58
#[ wasm_bindgen]
77
59
pub fn get_all_rule_names ( ) -> Box < [ JsValue ] > {
78
- rules:: get_all_rule_names ( )
60
+ darklua_core :: rules:: get_all_rule_names ( )
79
61
. into_iter ( )
80
62
. map ( JsValue :: from_str)
81
63
. collect :: < Vec < _ > > ( )
0 commit comments