Skip to content

Commit

Permalink
feat(lexer): datastructures and basic tests
Browse files Browse the repository at this point in the history
The tests still fail as the actual lexer implementation is still to be
done.
  • Loading branch information
Byron committed May 6, 2015
1 parent f3b4120 commit f66ea5f
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json-tools"
version = "0.0.1"
version = "0.1.0"
authors = ["Sebastian Thiel <[email protected]>"]
license = "MIT"
description = "A collections of tools to handle json encoded data"
Expand Down
85 changes: 83 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,84 @@
#[test]
fn it_works() {
/// A lexer for utf-8 encoded json data
pub struct Lexer<I>
where I: Iterator<Item=char> {
chars: I,
cursor: u64,
}

#[derive(Debug, PartialEq)]
pub enum TokenType {
/// All whitespace
WhiteSpace,

/// `{`
CurlyOpen,
/// `}`
CurlyClose,

/// `[`
BracketOpen,
/// `]`
BracketClose,

/// `:`
Colon,
/// `,`
Comma,


/// A json string , like `"foo"`
StringValue,
/// `true`
BooleanTrue,
/// `false`
BooleanFalse,
/// any json number, like `1.24123` or `123`
Number,
/// `null`
NullValue,

/// The type of the token could not be identified.
/// Should be removed if this lexer is ever to be feature complete
Invalid,
}

/// A pair of indices into the character stream returned by our source
/// iterator.
/// It is an exclusive range.
#[derive(Debug, PartialEq)]
pub struct Span {
/// Index of the first the character
pub first: u64,
/// Index one past the last character
pub end: u64,
}

/// A lexical token, identifying its kind and span.
#[derive(Debug, PartialEq)]
pub struct Token {
/// The exact type of the token
pub kind: TokenType,
/// The span allows to reference back into the source character stream
/// to obtain the string making up the token.
pub span: Span,
}

impl<I> Lexer<I> where I: Iterator<Item=char> {
/// Returns a new Lexer from a given character iterator.
pub fn new(chars: I) -> Lexer<I> {
Lexer {
chars: chars,
cursor: 0,
}
}
}

impl<I> Iterator for Lexer<I>
where I: Iterator<Item=char> {
type Item = Token;

/// Lex the underlying character stream to generate tokens
fn next(&mut self) -> Option<Token> {
None
}
}
25 changes: 25 additions & 0 deletions tests/lexer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
extern crate json_tools;

use json_tools::{Lexer, Token, Span, TokenType};

#[test]
fn unicode() {
let src = r#"{ "face": "😂" }"#;
let mut it = Lexer::new(src.chars());

assert_eq!(it.next(), Some(Token { kind: TokenType::BracketLeft,
span: Span { first: 0,
end: 1 } }));
}


#[test]
fn string_escaping() {
// Add code here
}


#[test]
fn multi_line_strings() {
// Add code here
}

0 comments on commit f66ea5f

Please sign in to comment.