Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Atoi example for Rust #98

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions examples/rust/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
RAGEL_RUST = ragel-rust
RUSTC = rustc
RUST_RAGEL_SOURCES = $(wildcard *.rs.rl)
EXECUTABLES = $(RUST_RAGEL_SOURCES:.rs.rl=_rust)
INTERMEDIATES = $(RUST_RAGEL_SOURCES:.rs.rl=.rs)
KEEP_INTERMEDIATES ?= 0

ifneq ($(KEEP_INTERMEDIATES),0)
$(info [BUILD] Keeping intermediate sources)
.PRECIOUS: %.rs
else
$(info [BUILD] Not keeping intermediate sources)
.INTERMEDIATE: %.rs
endif

$(info [BUILD] Executables: $(EXECUTABLES))

all: $(EXECUTABLES)

clean:
@echo [CLEAN]
find . -name '*.o' | xargs -n 1 rm -f
find . -name '*.ri' | xargs -n 1 rm -f
rm -f $(EXECUTABLES)
rm -f $(INTERMEDIATES)

%_rust: %.rs
@echo [RUSTC] $@
@$(RUSTC) $< -o $@

%.rs: %.rs.rl
@echo [RAGEL] $@
@$(RAGEL_RUST) $< -o $*.rs
11 changes: 11 additions & 0 deletions examples/rust/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Ragel source examples for Rust

This directory contains examples of building Rust parsers with Ragel.

# Contibution

Please consider the following build system-related constraints when adding
examples of your own, to seamlessly integrate your examples into build process.

1. One file -- one example;
2. Use `*.rs.rl` extension for your examples;
64 changes: 64 additions & 0 deletions examples/rust/atoi.rs.rl
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Convert a string to an integer.
//

%%{
machine atoi;
write data;
}%%

fn atoi(data: &[u8]) -> i32
{
let mut cs: i32 = 0;
let mut p = 0usize; // Start position
let mut pe = data.len(); // End position
let mut val = 0i32; // Accumulated value
let mut neg = false; // Negative number flag

%%{
action see_neg {
neg = true;
}

# Take a look at `fc`. It denotes current symbol
action add_digit {
val = val * 10 + (fc - ('0' as u8)) as i32;
}

main :=
( '-'@see_neg | '+' )? ( digit @add_digit )+
'\n';

# Initialize and execute.
write init;
write exec;
}%%

if neg {
val = -val;
}

if cs < atoi_first_final {
println!("atoi: error");
}

return val;
}


const BUFSIZE: usize = 1024;

fn main() {
use std::io::BufRead;
use std::convert::TryInto;

let stdin = std::io::stdin();
let mut handle = stdin.lock();

loop {
let mut buffer = String::new();
handle.read_line(&mut buffer);
let result = atoi(buffer.as_bytes());
println!("{}", result);
}
}