Skip to content
Snippets Groups Projects
Commit 0085ed11 authored by Carlos Valencia's avatar Carlos Valencia
Browse files

Parsed a character

parent 4d05e57d
No related branches found
No related tags found
No related merge requests found
/target
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "rustec"
version = "0.1.0"
[package]
name = "rustec"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
a
\ No newline at end of file
use std::env;
use std::fs;
mod parser_impls;
use parser_impls::chars::CharacterParser;
mod parser;
use parser::parser::Parser;
fn main() {
let source = "src/examples/example_programs/character.txt";
let file = fs::read_to_string(source).expect("File was not found or could not be read");
let parse_a = CharacterParser{};
let res = Parser::parse(&parse_a, file);
print!("{:#?}", res);
}
pub mod parser;
pub(crate) struct Context {
pub row: usize,
pub col: usize,
}
use super::Context;
pub(crate) trait Parser {
// The parsing function resturns a tuple (Parsed, Rest) or an Error String
fn parse_fun(&self, input: String, ctx: &mut Context) -> Result<(String, String), String>;
fn parse(&self, input: String) -> Result<(String,String) , String> {
let mut ctx = Context{
row: 0,
col: 0
};
self.parse_fun(input, &mut ctx)
}
}
\ No newline at end of file
use crate::Parser;
use crate::parser::Context;
pub(crate) struct CharacterParser;
impl Parser for CharacterParser {
fn parse_fun(&self, input: String, ctx: &mut Context) -> Result<(String, String), String> {
if input.len() == 0 {
return Err("Empty string".to_owned());
}
let first = &input[..1];
let rest = &input[1..];
return Ok((first.to_owned(), rest.to_owned()));
}
}
pub mod chars;
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment