Skip to content

Instantly share code, notes, and snippets.

@hamadu
Created April 8, 2017 00:19
Show Gist options
  • Save hamadu/45dea9365dd5fd99aa8cf96892e5474e to your computer and use it in GitHub Desktop.
Save hamadu/45dea9365dd5fd99aa8cf96892e5474e to your computer and use it in GitHub Desktop.
use std::io;
// fn tokenize(line: CharIndices) {
// }
//
// fn parse(line: &str, l: usize) {
// let n = line.len();
// let mut idx = l;
//
// let indices = line.char_indices();
// idx += 1;
// while idx < n {
// idx += 1;
// }
// }
fn token_space(line: &Vec<char>, head: usize) -> usize {
let mut pos = head;
while line[pos] == ' ' {
pos += 1;
}
pos
}
fn token_colon(line: &Vec<char>, head: usize) -> usize {
let mut pos = head;
while line[pos] != ':' {
pos += 1;
}
pos
}
fn token_string(line: &Vec<char>, head: usize) -> (usize, usize) {
let left = token_space(line, head);
let mut pos = left;
let p = line[pos];
pos += 1;
while line[pos] != p {
pos += 1;
}
(left, pos)
}
fn tokenize(line: &Vec<char>, head: usize) -> usize {
let n = line.len();
if head >= n {
return head
}
let mut pos = head;
while pos < n && line[pos] == ' ' {
pos += 1;
}
if line[pos] != '{' {
panic!("parse error at {}-th character", head);
}
pos += 1;
let mut key: Vec<(usize, usize)> = Vec::new();
let mut value: Vec<(usize, usize)> = Vec::new();
while pos < n {
let key_token = token_string(line, pos);
key.push(key_token);
pos = key_token.1;
pos = token_colon(line, pos);
let value_token = token_string(line, pos);
value.push(value_token);
}
1
}
fn main() {
let mut line = String::new();
io::stdin().read_line(&mut line).expect("Failed to receive input");
let data: Vec<char> = line.chars().collect();
tokenize(&data, 0);
println!("in: {}", line);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment