1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*!
# Pest IDE

This is a bridge library for the [IntelliJ IDEA plugin for Pest][jb].

 [jb]: https://plugins.jetbrains.com/plugin/12046-pest
 [asmble]:https://github.com/cretz/asmble

It's supposed to be compiled only with the wasm32 backend of nightly rustc
(at least at this moment).

After compiling as wasm, it's translated to JVM bytecode with [asmble][asmble] and then
loaded in the plugin.
Thus no JNI.
*/

#![feature(box_syntax, box_patterns)]

use std::alloc::System;
use std::ffi::CString;
use std::{mem, str};

use pest::error::{Error, ErrorVariant, InputLocation};
use pest::iterators::Pair;
use pest_meta::parser::{self, Rule};
use pest_meta::{optimizer, validator};
use pest_vm::Vm;

use self::misc::JavaStr;

/// Allocation library for Java use.
///
/// On Java side, it can create a Rust string based on the codes in this module.
pub mod str4j;

/// Everything that are not related to pest.
pub mod misc;

#[global_allocator]
static GLOBAL_ALLOCATOR: System = System;
static mut VM: Option<Vm> = None;

/// From position to line-column pair.
fn line_col(pos: usize, input: &str) -> (usize, usize) {
    let mut pos = pos;
    // Position's pos is always a UTF-8 border.
    let slice = &input[..pos];
    let mut chars = slice.chars().peekable();

    let mut line_col = (1, 1);

    while pos != 0 {
        match chars.next() {
            Some('\r') => {
                if let Some(&'\n') = chars.peek() {
                    chars.next();

                    if pos == 1 {
                        pos -= 1;
                    } else {
                        pos -= 2;
                    }

                    line_col = (line_col.0 + 1, 1);
                } else {
                    pos -= 1;
                    line_col = (line_col.0, line_col.1 + 1);
                }
            }
            Some('\n') => {
                pos -= 1;
                line_col = (line_col.0 + 1, 1);
            }
            Some(c) => {
                pos -= c.len_utf8();
                line_col = (line_col.0, line_col.1 + 1);
            }
            None => unreachable!(),
        }
    }

    line_col
}

/// Convert the error to a readable format.
fn convert_error(error: Error<Rule>, grammar: &str) -> String {
    let message = match error.variant {
        ErrorVariant::CustomError { message } => message,
        _ => unreachable!(),
    };
    let ((start_line, start_col), (end_line, end_col)) = match error.location {
        InputLocation::Pos(pos) => (line_col(pos, grammar), line_col(pos, grammar)),
        InputLocation::Span((start, end)) => (line_col(start, grammar), line_col(end, grammar)),
    };
    format!(
        "{:?}^{:?}^{:?}^{:?}^{}",
        start_line, start_col, end_line, end_col, message
    )
}

#[no_mangle]
/// Load the Pest VM as a global variable.
pub extern "C" fn load_vm(pest_code: JavaStr, pest_code_len: i32) -> JavaStr {
    let pest_code_len = pest_code_len as usize;
    let pest_code_bytes =
        unsafe { Vec::<u8>::from_raw_parts(pest_code, pest_code_len, pest_code_len) };
    let pest_code = str::from_utf8(&pest_code_bytes).unwrap();
    let pest_code_result = parser::parse(Rule::grammar_rules, pest_code).map_err(|error| {
        error.renamed_rules(|rule| match *rule {
            Rule::grammar_rule => "rule".to_owned(),
            Rule::_push => "push".to_owned(),
            Rule::assignment_operator => "`=`".to_owned(),
            Rule::silent_modifier => "`_`".to_owned(),
            Rule::atomic_modifier => "`@`".to_owned(),
            Rule::compound_atomic_modifier => "`$`".to_owned(),
            Rule::non_atomic_modifier => "`!`".to_owned(),
            Rule::opening_brace => "`{`".to_owned(),
            Rule::closing_brace => "`}`".to_owned(),
            Rule::opening_paren => "`(`".to_owned(),
            Rule::positive_predicate_operator => "`&`".to_owned(),
            Rule::negative_predicate_operator => "`!`".to_owned(),
            Rule::sequence_operator => "`&`".to_owned(),
            Rule::choice_operator => "`|`".to_owned(),
            Rule::optional_operator => "`?`".to_owned(),
            Rule::repeat_operator => "`*`".to_owned(),
            Rule::repeat_once_operator => "`+`".to_owned(),
            Rule::comma => "`,`".to_owned(),
            Rule::closing_paren => "`)`".to_owned(),
            Rule::quote => "`\"`".to_owned(),
            Rule::insensitive_string => "`^`".to_owned(),
            Rule::range_operator => "`..`".to_owned(),
            Rule::single_quote => "`'`".to_owned(),
            other_rule => format!("{:?}", other_rule),
        })
    });
    let pairs = match pest_code_result {
        Ok(pairs) => pairs,
        Err(err) => {
            let cstr = CString::new(format!("Err[{:?}]", convert_error(err, &pest_code))).unwrap();
            let ptr = cstr.as_ptr() as *mut _;
            mem::forget(pest_code_bytes);
            mem::forget(cstr);
            return ptr;
        }
    };

    if let Err(errors) = validator::validate_pairs(pairs.clone()) {
        let cstr = CString::new(format!(
            "Err{:?}",
            errors
                .into_iter()
                .map(|e| convert_error(e, &pest_code))
                .collect::<Vec<_>>()
        ))
        .unwrap();
        let ptr = cstr.as_ptr() as *mut _;
        mem::forget(pest_code_bytes);
        mem::forget(cstr);
        return ptr;
    }

    let ast = match parser::consume_rules(pairs) {
        Ok(ast) => ast,
        Err(errors) => {
            let cstr = CString::new(format!(
                "Err{:?}",
                errors
                    .into_iter()
                    .map(|e| convert_error(e, &pest_code))
                    .collect::<Vec<_>>()
            ))
            .unwrap();
            let ptr = cstr.as_ptr() as *mut _;
            mem::forget(pest_code_bytes);
            mem::forget(cstr);
            return ptr;
        }
    };

    let rules: Vec<_> = ast.iter().map(|rule| rule.name.clone()).collect();
    unsafe {
        VM = Some(Vm::new(optimizer::optimize(ast)));
    }

    let cstr = CString::new(format!("{:?}", rules)).unwrap();
    let ptr = cstr.as_ptr() as *mut _;
    mem::forget(pest_code_bytes);
    mem::forget(cstr);
    ptr
}

/// Convert the pair information to a string that the plugin
/// understands.
fn join_pairs(result: &mut Vec<String>, pair: Pair<&str>) {
    let span = pair.as_span();
    let start = span.start();
    let end = span.end();
    result.push(format!("{:?}^{:?}^{}", start, end, pair.as_rule()));
    for child in pair.into_inner() {
        join_pairs(result, child);
    }
}

#[no_mangle]
/// After loading the VM, this function can parse the code with the
/// currently loaded VM.
/// Assumes the VM is already loaded, otherwise it'll panic.
pub extern "C" fn render_code(
    rule_name: JavaStr,
    rule_name_len: i32,
    user_code: JavaStr,
    user_code_len: i32,
) -> JavaStr {
    let vm = unsafe { VM.as_ref().unwrap() };
    let rule_name_len = rule_name_len as usize;
    let rule_name_bytes =
        unsafe { Vec::<u8>::from_raw_parts(rule_name, rule_name_len, rule_name_len) };
    let rule_name = str::from_utf8(&rule_name_bytes).unwrap();
    let user_code_len = user_code_len as usize;
    let user_code_bytes =
        unsafe { Vec::<u8>::from_raw_parts(user_code, user_code_len, user_code_len) };
    let user_code = str::from_utf8(&user_code_bytes).unwrap();
    let cstr = CString::new(match vm.parse(rule_name, user_code) {
        Ok(pairs) => {
            let mut result = vec![];
            for pair in pairs {
                join_pairs(&mut result, pair);
            }
            format!("{:?}", result)
        }
        Err(err) => format!("Err{}", err.renamed_rules(|r| r.to_string())),
    })
    .unwrap();
    mem::forget(rule_name_bytes);
    mem::forget(user_code_bytes);
    let ptr = cstr.as_ptr() as *mut _;
    mem::forget(cstr);
    ptr
}