public IEnumerable <BQueryToken> GetTokens() { while (!this.textSource.IsEnded) { char c = this.textSource.Current; if (c == '.') { yield return(new BQueryToken(BQueryTokenKind.Dot, ".", this.textSource.GetPosition())); } else if (c == '[') { yield return(new BQueryToken(BQueryTokenKind.LBracket, "[", this.textSource.GetPosition())); } else if (c == ']') { yield return(new BQueryToken(BQueryTokenKind.RBracket, "]", this.textSource.GetPosition())); } else { if (char.IsLetter(c)) { var(ident, offset) = LexerHelper.GetAnyName(this.textSource.Slice()); yield return(new BQueryToken(BQueryTokenKind.Id, ident, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } else if (char.IsDigit(c)) { var(numberString, offset) = LexerHelper.GetNumber(this.textSource.Slice()); yield return(new BQueryToken(BQueryTokenKind.Number, numberString, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } else if (c == '\'') { var(str, offset) = LexerHelper.GetSingleQuoteString(this.textSource.Slice()); yield return(new BQueryToken(BQueryTokenKind.String, str, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } else if (c == '\"') { var(str, offset) = LexerHelper.GetDoubleQuoteString(this.textSource.Slice()); yield return(new BQueryToken(BQueryTokenKind.String, str, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } } this.textSource.AdvanceChar(); } }
public IFunctionsRepository AddComplexFunction <T>(string keyWord, params object[] args) where T : ComplexFunctionBase { if (!LexerHelper.IsCorrectIdentifier(keyWord)) { throw new ScriptInitException($"Keyword '{keyWord}' is not correct identifier!"); } if (!IsNameAvalible(keyWord)) { throw new ScriptInitException($"Keyword '{keyWord}' already used!"); } complexFunctions[keyWord.ToLower()] = new FunctionStorage(typeof(T), args); return(this); }
public bool IsNameAvalible(string name) { name = name.ToLower(); if (LexerHelper.IsTokenSeparator(name)) { return(false); } if (operations.ContainsKey(name)) { return(false); } if (complexFunctions.ContainsKey(name)) { return(false); } if (functions.ContainsKey(name)) { return(false); } return(true); }
public IEnumerable <LispToken> GetTokens() { HashSet <char> charsSymbols = new HashSet <char>() { '+', '*', '/', //math '=', '<', '>' //boolean }; while (!this.textSource.IsEnded) { var c = this.textSource.Current; if (c == ';') { //comment ignore until new line do { this.textSource.AdvanceChar(); } while (!this.textSource.IsEnded && this.textSource.Current != '\n'); } else if (c == '(') { yield return(new LispToken(LispTokenKind.LParen, "(", this.textSource.GetPosition())); } else if (c == ')') { yield return(new LispToken(LispTokenKind.RParen, ")", this.textSource.GetPosition())); } else if (c == '\'') { yield return(new LispToken(LispTokenKind.Quote, "'", this.textSource.GetPosition())); } else if (charsSymbols.Contains(c)) { yield return(new LispToken(LispTokenKind.Symbol, c.ToString(), this.textSource.GetPosition())); } else if (c == '-') { if (char.IsDigit(this.textSource.Next)) { this.textSource.AdvanceChar(); // for the '-' symbol var(numberString, offset) = LexerHelper.GetNumber(this.textSource.Slice()); yield return(new LispToken(LispTokenKind.Number, '-' + numberString, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } else { yield return(new LispToken(LispTokenKind.Symbol, "-", this.textSource.GetPosition())); } } else { if (char.IsLetter(c)) { var match = Regex.Match(this.textSource.Slice().ToString(), @"[\w!_]+", RegexOptions.Compiled); yield return(new LispToken(LispTokenKind.Symbol, match.Value, this.textSource.GetPosition())); this.textSource.AdvanceChar(match.Length - 1); } else if (char.IsDigit(c)) { var(numberString, offset) = LexerHelper.GetNumber(this.textSource.Slice()); yield return(new LispToken(LispTokenKind.Number, numberString, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } else if (c == '\"') { var(str, offset) = LexerHelper.GetDoubleQuoteString(this.textSource.Slice()); yield return(new LispToken(LispTokenKind.String, str, this.textSource.GetPosition())); this.textSource.AdvanceChar(offset); } } this.textSource.AdvanceChar(); } }
public IEnumerable <CmdToken> GetTokens() { while (!this.textSource.IsEnded) { var currentChar = this.textSource.Current; if (currentChar == '=') { var nextChar = this.textSource.Next; if (nextChar == '=') { yield return(new CmdToken(CmdTokenKind.CompareEqual, "==")); this.textSource.AdvanceChar(); } else { yield return(new CmdToken(CmdTokenKind.EqualSign, "=")); } } else if (currentChar == '<') { var nextChar = this.textSource.Next; if (nextChar == '=') { yield return(new CmdToken(CmdTokenKind.LessThanEqual, "<=")); this.textSource.AdvanceChar(); } else { yield return(new CmdToken(CmdTokenKind.LessThan, "<")); } } else if (currentChar == '>') { var nextChar = this.textSource.Next; if (nextChar == '=') { yield return(new CmdToken(CmdTokenKind.GreaterThanEqual, ">=")); this.textSource.AdvanceChar(); } else { yield return(new CmdToken(CmdTokenKind.GreaterThan, ">")); } } else if (currentChar == '+') { yield return(new CmdToken(CmdTokenKind.Plus, "+")); } else if (currentChar == '-') { yield return(new CmdToken(CmdTokenKind.Minus, "-")); } else if (currentChar == '/') { yield return(new CmdToken(CmdTokenKind.Slash, "/")); } else if (currentChar == '*') { yield return(new CmdToken(CmdTokenKind.Star, "*")); } else if (currentChar == '(') { yield return(new CmdToken(CmdTokenKind.LParen, "(")); } else if (currentChar == ')') { yield return(new CmdToken(CmdTokenKind.RParen, ")")); } else if (currentChar == ',') { yield return(new CmdToken(CmdTokenKind.Comma, ",")); } else if (currentChar == '.') { yield return(new CmdToken(CmdTokenKind.Dot, ".")); } else if (currentChar == '!') { var nextChar = this.textSource.Next; if (nextChar == '=') { yield return(new CmdToken(CmdTokenKind.CompareNotEqual, "!=")); this.textSource.AdvanceChar(); } } else { if (char.IsLetter(currentChar)) { var(ident, offset) = LexerHelper.GetAnyName(this.textSource.Slice()); yield return(MakeToken(ident)); this.textSource.AdvanceChar(offset); } else if (char.IsDigit(currentChar)) { var(numberString, offset) = LexerHelper.GetNumber(this.textSource.Slice()); yield return(new CmdToken(CmdTokenKind.Number, numberString)); this.textSource.AdvanceChar(offset); } else if (currentChar == '\'') { var(str, offset) = LexerHelper.GetSingleQuoteString(this.textSource.Slice()); yield return(new CmdToken(CmdTokenKind.String, str)); this.textSource.AdvanceChar(offset); } else if (currentChar == '\"') { var(str, offset) = LexerHelper.GetDoubleQuoteString(this.textSource.Slice()); yield return(new CmdToken(CmdTokenKind.String, str)); this.textSource.AdvanceChar(offset); } } this.textSource.AdvanceChar(); } }
public void GetNextToken() { ThirdLastToken = SecondLastToken; SecondLastToken = LastToken; LastToken = CurrentToken; //Skips whitespace in expression while (_index < _length && (_expression[_index].ToString() == " " || _expression[_index] == '\t' || _expression[_index] == '\r' || _expression[_index] == '\n')) { if (_expression[_index] == '\n') { LexerHelper.LineNumber++; } _index++; } if (_index == _length) { CurrentToken = Token.EndFile; return; } //Check if current index of expression is a statement if (LexerHelper.IsStartOfStatement(_expression[_index])) { string statementString = ""; while (_expression[_index].ToString() != " " && _expression[_index] != ';' && _expression[_index] != ',') { statementString += _expression[_index]; _index++; } if (_tokenValues.Any(x => x.Value == statementString)) { CurrentToken = _tokenValues.First(x => x.Value == statementString).Key; return; } } //Checks if current index of expression is start of a quoted string expression if (LexerHelper.IsStartOfString(_expression[_index])) { string str = ""; _index++; while (_expression[_index] != '\"') { str += _expression[_index]; _index++; } CurrentToken = Token.String; StringValue = str; _index++; return; } //Checks if current index is a single char token /* * if (_singleCharValues.Any(x => x.Value == _expression[_index])) * { * CurrentToken = _singleCharValues.First(x => x.Value == _expression[_index]).Key; * _index++; * return; * } */ switch (_expression[_index]) { case '+': CurrentToken = Token.Plus; _index++; return; case '-': CurrentToken = Token.Minus; _index++; return; case '/': CurrentToken = Token.Divide; _index++; return; case '*': CurrentToken = Token.Multiply; _index++; return; case '(': CurrentToken = Token.OpenBracket; _index++; return; case ')': CurrentToken = Token.CloseBracket; _index++; return; case '!': CurrentToken = Token.Not; _index++; return; case '=': CurrentToken = Token.VariableAssignment; _index++; return; case ';': CurrentToken = Token.Semi; _index++; return; case '{': CurrentToken = Token.OpenBrace; _index++; return; case '}': CurrentToken = Token.CloseBrace; _index++; return; case ',': CurrentToken = Token.Comma; _index++; return; } //Checks if current index of expression is start of variable name if (_expression[_index] == '$') { string variableName = ""; string[] charactersToIgnore = { " ", ";", ")", "+", "!", "(", "-", "*", "/", "{", "\r", "\n", "," }; while (!charactersToIgnore.Contains(_expression[_index].ToString())) { variableName += _expression[_index]; _index++; } CurrentToken = Token.VariableName; VariableName = variableName; return; } //Checks if current inde of expression is start of function name if (_expression[_index] == '@') { string functionName = ""; string[] charactersToIgnore = { " ", ";", ")", "+", "!", "(", "-", "*", "/", "\r", "\n" }; while (!charactersToIgnore.Contains(_expression[_index].ToString())) { functionName += _expression[_index]; _index++; } CurrentToken = Token.FunctionName; FunctionName = functionName; return; } //Checks if current index of expression is start of a numerical value if (LexerHelper.IsNumber(_expression[_index])) { string number = ""; while (LexerHelper.IsNumber(_expression[_index])) { number += _expression[_index]; _index++; } CurrentToken = Token.Number; NumberValue = Double.Parse(number); return; } throw new Exception("Unidentifiable token detected"); }