📄 lookaheadset.cs
字号:
/** * The list of token ids in this sequence. */ private ArrayList tokens = null; /** * Creates a new empty token sequence. The repeat flag * will be set to false. */ public Sequence() { this.repeat = false; this.tokens = new ArrayList(0); } /** * Creates a new token sequence with a single token. * * @param repeat the repeat flag value * @param token the token to add */ public Sequence(bool repeat, int token) { this.repeat = false; this.tokens = new ArrayList(1); this.tokens.Add(token); } /** * Creates a new token sequence that is a duplicate of * another sequence. Only a limited number of tokens will * be copied however. The repeat flag from the original * will be kept intact. * * @param length the maximum number of tokens to copy * @param seq the sequence to copy */ public Sequence(int length, Sequence seq) { this.repeat = seq.repeat; this.tokens = new ArrayList(length); if (seq.Length() < length) { length = seq.Length(); } for (int i = 0; i < length; i++) { tokens.Add(seq.tokens[i]); } } /** * Creates a new token sequence that is a duplicate of * another sequence. The new value of the repeat flag will * be used however. * * @param repeat the new repeat flag value * @param seq the sequence to copy */ public Sequence(bool repeat, Sequence seq) { this.repeat = repeat; this.tokens = seq.tokens; } /** * Returns the length of the token sequence. * * @return the number of tokens in the sequence */ public int Length() { return tokens.Count; } /** * Returns a token at a specified position in the sequence. * * @param pos the sequence position * * @return the token id found, or null */ public object GetToken(int pos) { if (pos >= 0 && pos < tokens.Count) { return tokens[pos]; } else { return null; } } /** * Checks if this sequence is equal to another object. * Only token sequences with the same tokens in the same * order will be considered equal. The repeat flag will be * disregarded. * * @param obj the object to compare with * * @return true if the objects are equal, or * false otherwise */ public override bool Equals(object obj) { if (obj is Sequence) { return Equals((Sequence) obj); } else { return false; } } /** * Checks if this sequence is equal to another sequence. * Only sequences with the same tokens in the same order * will be considered equal. The repeat flag will be * disregarded. * * @param seq the sequence to compare with * * @return true if the sequences are equal, or * false otherwise */ public bool Equals(Sequence seq) { if (tokens.Count != seq.tokens.Count) { return false; } for (int i = 0; i < tokens.Count; i++) { if (!tokens[i].Equals(seq.tokens[i])) { return false; } } return true; } /** * Checks if this token sequence starts with the tokens from * another sequence. If the other sequence is longer than this * sequence, this method will always return false. * * @param seq the token sequence to check * * @return true if this sequence starts with the other, or * false otherwise */ public bool StartsWith(Sequence seq) { if (Length() < seq.Length()) { return false; } for (int i = 0; i < seq.tokens.Count; i++) { if (!tokens[i].Equals(seq.tokens[i])) { return false; } } return true; } /** * Checks if this token sequence is repetitive. A repetitive * token sequence is one with the repeat flag set. * * @return true if this token sequence is repetitive, or * false otherwise */ public bool IsRepetitive() { return repeat; } /** * Checks if the next token(s) in the parser matches this * token sequence. * * @param parser the parser to check * * @return true if the next tokens are in the sequence, or * false otherwise */ public bool IsNext(Parser parser) { Token token; int id; for (int i = 0; i < tokens.Count; i++) { id = (int) tokens[i]; token = parser.PeekToken(i); if (token == null || token.GetId() != id) { return false; } } return true; } /** * Checks if the next token(s) in the parser matches this * token sequence. * * @param parser the parser to check * @param length the maximum number of tokens to check * * @return true if the next tokens are in the sequence, or * false otherwise */ public bool IsNext(Parser parser, int length) { Token token; int id; if (length > tokens.Count) { length = tokens.Count; } for (int i = 0; i < length; i++) { id = (int) tokens[i]; token = parser.PeekToken(i); if (token == null || token.GetId() != id) { return false; } } return true; } /** * Returns a string representation of this object. * * @return a string representation of this object */ public override string ToString() { return ToString(null); } /** * Returns a string representation of this object. * * @param tokenizer the tokenizer containing the tokens * * @return a string representation of this object */ public string ToString(Tokenizer tokenizer) { StringBuilder buffer = new StringBuilder(); string str; int id; if (tokenizer == null) { buffer.Append(tokens.ToString()); } else { buffer.Append("["); for (int i = 0; i < tokens.Count; i++) { id = (int) tokens[i]; str = tokenizer.GetPatternDescription(id); if (i > 0) { buffer.Append(" "); } buffer.Append(str); } buffer.Append("]"); } if (repeat) { buffer.Append(" *"); } return buffer.ToString(); } /** * Creates a new token sequence that is the concatenation * of this sequence and another. A maximum length for the * new sequence is also specified. * * @param length the maximum length of the result * @param seq the other sequence * * @return the concatenated token sequence */ public Sequence Concat(int length, Sequence seq) { Sequence res = new Sequence(length, this); if (seq.repeat) { res.repeat = true; } length -= this.Length(); if (length > seq.Length()) { res.tokens.AddRange(seq.tokens); } else { for (int i = 0; i < length; i++) { res.tokens.Add(seq.tokens[i]); } } return res; } /** * Creates a new token sequence that is a subsequence of * this one. * * @param start the subsequence start position * * @return the new token subsequence */ public Sequence Subsequence(int start) { Sequence res = new Sequence(Length(), this); while (start > 0 && res.tokens.Count > 0) { res.tokens.RemoveAt(0); start--; } return res; } } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -