Skip to content

Instantly share code, notes, and snippets.

@RyanSquared
Created March 30, 2018 20:38
Show Gist options
  • Save RyanSquared/7d5bde7fcf9cd22556fddfe601ed5ade to your computer and use it in GitHub Desktop.
Save RyanSquared/7d5bde7fcf9cd22556fddfe601ed5ade to your computer and use it in GitHub Desktop.
[package]
name = "testingstuffs"
version = "0.1.0"
authors = ["Ryan"]
[features]
debug = []
[dependencies]
pest = "~1.0"
pest_derive = "~1.0"
extern crate pest;
#[macro_use]
extern crate pest_derive;
use std::io::{self, BufRead, Write};
use pest::Parser;
use pest::iterators::Pairs;
#[cfg(feature = "debug")]
const _GRAMMAR: &'static str = include_str!("parser.pest");
#[derive(Parser)]
#[grammar = "parser.pest"]
struct IdentParser;
fn do_thing(pairs: Pairs<Rule>) {
for pair in pairs {
println!("Rule: {:?}", pair.as_rule());
println!("Span: {:?}", pair.clone().into_span());
println!("Text: {}", pair.clone().into_span().as_str());
let rule = pair.as_rule();
if rule != Rule::statement {
panic!("Logic error; expected Rule::statement, got: {:?}", rule);
};
let mut inner_pairs = pair.into_inner();
let statement = inner_pairs.next().expect("No values captured in statement");
match statement.as_rule() {
Rule::assignment => {
let mut inner_pairs = statement.into_inner();
let (head_pair, tail_pair) = (inner_pairs.next(), inner_pairs.next());
let head = head_pair.expect("Missing head for assignment");
let tail = tail_pair.expect("Missing tail for assignment");
println!("tail: {:?}", tail.clone().into_inner().collect::<Vec<_>>());
println!("Setting {:?} to {:?}",
head.clone().into_span().as_str(),
tail.clone().into_span().as_str());
},
_ => panic!("Match not found for statement type: {:?}", rule)
};
}
}
fn main() {
let stdin = io::stdin();
let mut stdout = io::stdout();
print!("> ");
stdout.flush().unwrap();
let line = stdin.lock().lines().next().expect("Missing line").unwrap();
let pairs = IdentParser::parse(Rule::program, line.as_str()).unwrap();
do_thing(pairs);
}
//
// :GUIDE:
//
// Solitary units should be prepended by a $ to avoid having space included
// after the text.
//
whitespace = _{ " " }
// Match Lua's parsing rules
variable = { !'0'..'9' ~ ('a'..'z'|'A'..'Z'|"_"|'0'..'9')+ }
number = { '0'..'9'+ }
head = ${ variable }
tail = ${ variable | number }
assignment = { "local" ~ head ~ "=" ~ tail }
statement = { (assignment) ~ ";" }
program = _{ statement+ ~ eoi }
@RyanSquared
Copy link
Author

Dismissed; this will be useful later when adding in more potential statements

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment