using System; using System.Collections.Generic; namespace sharp.parser { public class Lexer { List tokenDefinitions = new List(); public Lexer(){ } public Lexer(TokenDefinition[] tokenDefinitions) { this.tokenDefinitions.AddRange(tokenDefinitions); } public void AddTokenDefinition(TokenDefinition tokenDefinition) { this.tokenDefinitions.Add(tokenDefinition); } public void RemoveTokenDefinition(TokenDefinition tokenDefinition) { this.tokenDefinitions.Remove(tokenDefinition); } public Token[] parse(CharBuffer buffer){ List tokens = new List(); while (!buffer.EndOfBuffer()){ Token t = null; foreach (TokenDefinition tdef in tokenDefinitions) { t = tdef.tryParse(buffer); if (t != null) { break; } } if (t == null) { break; } tokens.Add(t); } if (!buffer.EndOfBuffer()) { throw new FormatException(String.Format("Unexpected character at line {0} position {1}. '{2}'", buffer.CurrentLineNumber, buffer.CurrentLinePosition, buffer.Current)); } return tokens.ToArray(); } } }