private void AddToken(int lineNo, Match match) { var group1 = match.Groups[1]; var value = group1.Value; if (group1.Success) // if not a space { if (!match.Groups[2].Success) { // if not a comment Token token; if (match.Groups[3].Success) { token = new NumToken(lineNo, int.Parse(value)); } else if (match.Groups[4].Success) { token = new StrToken(lineNo, ToStringLiteral(value)); } else { token = new IdToken(lineNo, value); } queue.Add(token); } } }
protected void addToken(int lineNo, Match matcher) { string m = matcher.Groups[1].ToString(); if (m != "") { if (matcher.Groups[2].ToString() == "") { Token token; if (matcher.Groups[3].ToString() != "") { token = new NumToken(lineNo, int.Parse(m)); } else if (matcher.Groups[4].ToString() != "") { token = new StrToken(lineNo, toStringLiteral(m)); } else { token = new IdToken(lineNo, m); } queue.Add(token); } } }
public virtual Token Scan() { while (char.IsWhiteSpace(Peek()) && Match(Peek())) { } if (EOF) { return new Token { Type = TokenType.EOF, Line = _line, Col = _col } } ; if (Peek() == '[') { Match('['); return(new Token { Type = TokenType.OpenBracket, Line = _line, Col = _col }); } if (Peek() == ']') { Match(']'); return(new Token { Type = TokenType.CloseBracket, Line = _line, Col = _col }); } if (Peek() == ',') { Match(','); return(new Token { Type = TokenType.Coma, Line = _line, Col = _col }); } if (char.IsDigit(Peek())) { NumToken num = new NumToken { Line = _line, Col = _col }; while (char.IsDigit(Peek())) { num.Value *= 10; num.Value += int.Parse(Peek().ToString()); Match(Peek()); } return(num); } throw new ArgumentException($"Unexpected `{Peek()}` got at pos {_current}"); } }
/// <summary> /// 往队列中添加一个Token /// </summary> /// <param name="lineNumber">Token所在行号</param> /// <param name="item">Token源字符串</param> private void AddToken(int lineNumber, Match item) { Token token = null; string strValue = item.Value.Trim(); var groups = item.Groups; if (groups["com"].Success) { // 注释,忽略掉 } else if (groups["id"].Success) { // 标识符 token = new IdToken(lineNumber, strValue); } else if (groups["num"].Success) { // 整形字面量 bool succ = false; if (strValue.StartsWith("(-")) { // 负数 string fixedStrValue = strValue.Substring(2, strValue.Length - 3); if (int.TryParse(fixedStrValue, out int val)) { token = new NumToken(lineNumber, -val); succ = true; } } else if (int.TryParse(strValue, out int val)) { token = new NumToken(lineNumber, val); succ = true; } if (!succ) { // 解析未成功 throw new ParseException("解析整形字面量失败"); } } else if (groups["str"].Success) { // 字符串字面量 token = new StrToken(lineNumber, strValue.Substring(1, strValue.Length - 2)); } else if (groups["pun"].Success) { // 运算符 token = new PunToken(lineNumber, strValue); } else if (groups["key"].Success) { // 关键字 token = new KeyToken(lineNumber, strValue); } else if (groups["sep"].Success) { // 分号和括号 token = new SepToken(lineNumber, strValue); } else if (groups["other"].Success) { // 不能识别的字符 throw new ParseException(new StrToken(lineNumber, strValue), "非法字符"); } if (token != null) { m_tokenQueue.Add(token); //Utils.LogInfo("添加了Token: " + token.GetText()); } }
private Token ProcessDieDef( StringScanner steve, Stack<string> opStack, List<Token> output, bool lastTokenWasNumber) { steve.TrySkip(); // move past the d\ if (!steve.HasNext()) throw new InvalidExpressionException("no die type given"); if (Char.IsDigit (steve.Peek ())) { // check that the syntax is valid before just trying to read it Token dieCount = new NumToken(1); if (lastTokenWasNumber) { // the last number was the die count, because it was followed by a 'd' dieCount = output.Last(); output.RemoveAt (output.Count - 1); } BigInteger dieType = steve.ReadLong(); // this is safe because we checked that the next char is a digit // we now know that die type and the die count, now we need to see if there are extra instructions for the roll long keepCount = 1; KeepStrategy keepstrat = KeepStrategy.ALL; if (steve.HasNext () && char.IsLetter (steve.Peek ()) && Char.ToLower (steve.Peek ()) != 'd') { char extension = Char.ToLower (steve.Read ()); if (extension == 'h') { keepstrat = KeepStrategy.HIGHEST; BigInteger temp = null; if (steve.TryReadLong (ref temp)) { if (temp > MAX_DIECOUNT) { throw new InvalidExpressionException (temp.ToString () + " is too many dice"); } else { keepCount = temp.LongValue (); } } } else if (extension == 'l') { keepstrat = KeepStrategy.LOWEST; BigInteger temp = null; if (steve.TryReadLong (ref temp)) { if (temp > MAX_DIECOUNT) { throw new InvalidExpressionException (temp.ToString () + " is too many dice"); } else { keepCount = temp.LongValue (); } } } else { throw new InvalidExpressionException("invalid die extension " + extension); } } var countList = new List<Token> (); countList.Add (dieCount); return new DiceToken (countList, dieType, keepCount, keepstrat, this); } else { throw new InvalidExpressionException("no die type given"); } }