aboutsummaryrefslogtreecommitdiff
path: root/examples/parselog.rs
diff options
context:
space:
mode:
authorJoe Wilm <joe@jwilm.com>2016-09-16 20:34:39 -0700
committerJoe Wilm <joe@jwilm.com>2016-09-16 20:34:39 -0700
commit930f8cc30a5bc4943c1b56e18cf1a3f8bb00bc2a (patch)
tree5a6419f22b7ec9f951c6629330f7a83f99db8a73 /examples/parselog.rs
parent5505121f6e67d1f39ff4d3aaea739b73e1a17345 (diff)
downloadr-alacritty-vte-930f8cc30a5bc4943c1b56e18cf1a3f8bb00bc2a.tar.gz
r-alacritty-vte-930f8cc30a5bc4943c1b56e18cf1a3f8bb00bc2a.tar.bz2
r-alacritty-vte-930f8cc30a5bc4943c1b56e18cf1a3f8bb00bc2a.zip
Implement first version of parser
Includes an example `parselog` which prints all of the actions a Parser implementation is given the opportunity to handle. One way to test this is to pipe vim into it: vim | target/release/examples/parselog And type `:q` to quit. Vim won't show up, but it still accepts input. This version of the parser doesn't handle UTF-8. It's implemented as described by http://vt100.net/emu/dec_ansi_parser which did not include UTF-8 support. Next steps are adding UTF-8 support.
Diffstat (limited to 'examples/parselog.rs')
-rw-r--r--examples/parselog.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/parselog.rs b/examples/parselog.rs
new file mode 100644
index 0000000..804c399
--- /dev/null
+++ b/examples/parselog.rs
@@ -0,0 +1,69 @@
+//! Parse input from stdin and log actions on stdout
+extern crate vtparse;
+
+use std::io::{self, Read};
+
+use vtparse::{StateMachine, Parser};
+
+/// A type implementing Parser that just logs actions
+struct Log;
+
+impl Parser for Log {
+ fn print(&mut self, _machine: &StateMachine, c: char) {
+ println!("[print] {:?}", c);
+ }
+ fn execute(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[execute] byte={:02x}", byte);
+ }
+ fn hook(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[hook] byte={:02x}", byte);
+ }
+ fn put(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[put] byte={:02x}", byte);
+ }
+ fn osc_start(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[osc_start] byte={:02x}", byte);
+ }
+ fn osc_put(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[osc_put] byte={:02x}", byte);
+ }
+ fn osc_end(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[osc_end] byte={:02x}", byte);
+ }
+ fn unhook(&mut self, _machine: &StateMachine, byte: u8) {
+ println!("[unhook] byte={:02x}", byte);
+ }
+ fn csi_dispatch(&mut self, machine: &StateMachine, c: char) {
+ println!("[csi_dispatch] params={:?}, intermediates={:?}, action={:?}",
+ machine.params(), machine.intermediates(), c);
+ }
+ fn esc_dispatch(&mut self, machine: &StateMachine, byte: u8) {
+ println!("[csi_dispatch] params={:?}, intermediates={:?}, action={:?}",
+ machine.params(), machine.intermediates(), byte as char);
+ }
+}
+
+fn main() {
+ let input = io::stdin();
+ let mut handle = input.lock();
+
+ let mut statemachine = StateMachine::new();
+ let mut parser = Log;
+
+ let mut buf: [u8; 2048] = unsafe { std::mem::uninitialized() };
+
+ loop {
+ match handle.read(&mut buf) {
+ Ok(0) => break,
+ Ok(n) => {
+ for byte in &buf[..n] {
+ statemachine.advance(&mut parser, *byte);
+ }
+ },
+ Err(err) => {
+ println!("err: {}", err);
+ break;
+ }
+ }
+ }
+}