using System; using System.Collections.Generic; namespace sharp.parser { public class LexerPathSegment { public CharGroup CharGroup { get; private set; } public bool MayFinish { get; set; } public LexerPathSegment[] Followers { get { return followers.ToArray(); } } private List followers = new List(); public LexerPathSegment(){ this.CharGroup = null; } public LexerPathSegment(LexerPathSegment follower) { this.CharGroup = null; this.AddFollower(follower); } public LexerPathSegment(CharGroup charGroup) { this.CharGroup = charGroup; } public LexerPathSegment(char[] chars){ this.CharGroup = new CharGroup(chars); } public LexerPathSegment(char ch) { this.CharGroup = new CharGroup(ch); } public LexerPathSegment(char first,char last) { this.CharGroup = new CharGroup(first,last); } public LexerPathSegment(CharGroup charGroup,bool mayFinish) { this.CharGroup = charGroup; this.MayFinish = mayFinish; } public LexerPathSegment(char[] chars,bool mayFinish) { this.CharGroup = new CharGroup(chars); this.MayFinish = mayFinish; } public LexerPathSegment(char ch,bool mayFinish) { this.CharGroup = new CharGroup(ch); this.MayFinish = mayFinish; } public LexerPathSegment(char first, char last,bool mayFinish) { this.CharGroup = new CharGroup(first, last); this.MayFinish = mayFinish; } public LexerPathSegment(CharGroup charGroup, LexerPathSegment follower) { this.CharGroup = charGroup; this.AddFollower(follower); } public LexerPathSegment(char[] chars, LexerPathSegment follower) { this.CharGroup = new CharGroup(chars); this.AddFollower(follower); } public LexerPathSegment(char ch, LexerPathSegment follower) { this.CharGroup = new CharGroup(ch); this.AddFollower(follower); } public LexerPathSegment(char first, char last, LexerPathSegment follower) { this.CharGroup = new CharGroup(first, last); this.AddFollower(follower); } public void AddFollower(LexerPathSegment path, params LexerPathSegment[] paths) { AddFollower(path); foreach (LexerPathSegment p in paths) { AddFollower(p); } } public void AddFollower(LexerPathSegment path) { followers.Add(path); } public void RemoveFollower(LexerPathSegment path) { followers.Remove(path); } public int walk(CharBuffer buffer){ if (this.CharGroup == null){ foreach (LexerPathSegment next in followers){ int n = next.walk(buffer); if (n > 0){ return n; } } return -1; } if (MayFinish && buffer.EndOfBuffer()){ return 1; } if (this.CharGroup.Contains(buffer.Current)){ buffer.MoveNext(); foreach (LexerPathSegment next in followers){ int n = next.walk(buffer); if (n > 0){ buffer.MoveBack(); return n + 1; } } buffer.MoveBack(); if (MayFinish){ return 1; } } return -1; } } }