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
|
//! This file provides the Emacs binding for the core.
#![allow(unused_imports)]
extern crate repcore;
use repcore::{
derive::stdag::{PFRecorder, STDAGParser},
grammar::{Grammar, GrammarInfo},
semiring::{IdentityRecorder, Nat, PFS},
tokenizer::to_tokenizer,
valuation::{Count, ParseForest},
};
use repcore::parser::Parser as PParser;
use repcore::parser::ParserReportConfig as PParserConfig;
// TODO: Write Emacs binding
// Let's design the API for Emacs to use.
//
// Emacs offers the grammar file or grammar string, and then we
// generate a parser.
//
// Thereafter when we want to parse some input, Emacs gives the input
// file name or the input file string to this package, and then the
// package gives back the parse result.
//
// Besides, we need a data type to deal with errors.
//
// Handling a string is not a problem for us, but to return a parser
// to Emacs, and to return the parse result, are kind of troublesome.
//
// To achieve this, we need data types for a parser and for the parse
// result. Since a parser is just an implementation detail and should
// not be of concern to the user, we mught just represent a parser as
// just a custom user-ptr in Emacs.
//
// As to the parse result, we return a set of tuples:
//
// (label, i, k, j),
//
// where LABEL is represented as a tuple
//
// (INDEX, STRING),
//
// where index is the index of the rule and the string is the name of
// the non-terminal or the terminal that is spanned by this packed
// node.
//
// This is not intended for the user to inspect and play with. The
// package should provide another function to interpret this data, for
// example to search something or to display it. The reason we don't
// directly return something to display is that the plain forest might
// contain cyclic data, and is inconvenient to represent in Emacs. So
// we only try to display this information to the user if the need
// arises.
#[derive(Debug, Clone)]
#[repr(C)]
pub struct Parser {
g: Grammar,
gi: GrammarInfo,
stparser: STDAGParser<PFS, ParseForest, PFRecorder>,
}
impl Parser {
fn new(gs: &str) -> Result<Self, Box<dyn std::error::Error>> {
let g: Grammar = gs.parse()?;
let mut stparser = STDAGParser::new();
let mut gi = g.generate_info()?;
stparser.init(&g, &mut gi)?;
Ok(Self { g, gi, stparser })
}
}
#[no_mangle]
extern "C" fn new_parser(
gs: *mut std::os::raw::c_char,
error: *mut std::os::raw::c_int,
) -> *mut Parser {
let parsed_str;
let parser_result;
unsafe {
let cstr = std::ffi::CStr::from_ptr(gs).to_str();
if cstr.is_err() {
*error = 1;
return std::ptr::null_mut();
}
parsed_str = cstr.unwrap().to_owned();
parser_result = Parser::new(&parsed_str);
if parser_result.is_err() {
*error = 2;
eprintln!("failed: {parser_result:?}");
return std::ptr::null_mut();
}
*error = 0;
}
Box::into_raw(Box::new(parser_result.unwrap()))
}
#[no_mangle]
extern "C" fn clean_parser(parser: *const std::ffi::c_void) {
unsafe {
Box::from_raw(parser as *mut Parser);
}
}
// #[no_mangle]
// extern "C" fn reset_parser(parser: *mut Parser) {
// let mut parser_box;
// unsafe {
// parser_box = Box::from_raw(parser);
// }
// parser_box
// .stparser
// .init(&parser_box.g, &parser_box.gi)
// .unwrap();
// std::mem::forget(parser_box);
// }
// FIXME: Use a pointer to int to indicate the type of errors, and use
// a pointer to a custom error struct to convey the error messages.
#[no_mangle]
extern "C" fn parser_parse(
parser: *mut Parser,
file: *const std::os::raw::c_char,
) -> std::os::raw::c_int {
let mut parser_box;
let cstr;
unsafe {
parser_box = Box::from_raw(parser);
cstr = std::ffi::CStr::from_ptr(file).to_str();
if cstr.is_err() {
eprintln!("error reading string: {:?}", cstr);
return 0;
}
}
parser_box.stparser.reinit();
let file_string = std::fs::read_to_string(cstr.unwrap());
if file_string.is_err() {
eprintln!("error reading file: {:?}", file_string);
return 0;
}
let file_string = file_string.unwrap();
let parse_result = parser_box
.stparser
.parse(&file_string.chars().collect::<Vec<_>>());
if parse_result.is_err() {
eprintln!("failed to parse: {:?}", parse_result);
return 0;
}
let (accepting, _) = parse_result.unwrap();
std::mem::forget(parser_box);
accepting as std::os::raw::c_int
}
#[no_mangle]
extern "C" fn parser_parse_string(
parser: *mut Parser,
input: *const std::os::raw::c_char,
) -> std::os::raw::c_int {
let mut parser_box;
let cstr;
unsafe {
parser_box = Box::from_raw(parser);
cstr = std::ffi::CStr::from_ptr(input).to_str();
if cstr.is_err() {
eprintln!("error reading string: {:?}", cstr);
return 0;
}
}
parser_box.stparser.reinit();
let file_string = cstr.unwrap().to_owned();
let parse_result = parser_box
.stparser
.parse(&file_string.chars().collect::<Vec<_>>());
if parse_result.is_err() {
eprintln!("failed to parse: {:?}", parse_result);
return 0;
}
let (accepting, _) = parse_result.unwrap();
std::mem::forget(parser_box);
accepting as std::os::raw::c_int
}
|