Writing a Markdown Parser Part 1. Lexing
struct Position {
line: usize;
column: usize;
}
enum Token {
eof,
illegal
}
struct Lexer<'a> {
source: Peekable<Chars<'a>>;
}
impl<'a> Lexer<'a> {
fn new(src: &str) {
let source = src.chars();
Lexer { source }
}
}
impl<'a> Iterator for Lexer<'a> {
type Item = (Token, Position);
fn next(&mut self) -> Self::Item {
match self.source.peek() {
Some(_) => None,
None => None,
}
}
}
Go
package lexer
type Iter struct {
position int
source []rune
}
func NewIter(source string) *Iter {
return &Iter{
position: 0,
source: []rune(source)
}
}
func (i *Iter) Next() (rune, bool) {
if (i.position) > len(source) {
return 0, false
}
r := i.source[i.position]
i.position++
return r, true
}
type Lexer struct {
Source *Iter
}
func NewLexer(input string) *Lexer {
source := NewIter(input)
return &Lexer{
Source: source
}
}