public override bool TryBypass(CompilationPool compilationPool) { var line = 1; while (compilationPool.CodePosition < compilationPool.Code.Length) { if (Constraints.Instance.Tokens.SkippedSymbols.Contains( compilationPool.Code[compilationPool.CodePosition])) { if (compilationPool.Code[compilationPool.CodePosition] == '\n') { line++; } compilationPool.CodePosition++; continue; } if (_stateMachines.Any(stateMachine => stateMachine.FindToken(compilationPool))) { OnTokenFounded(compilationPool.Tokens.Last(), compilationPool.FileName, line); continue; } if (LiteralParser.IsLiteral(compilationPool)) { OnTokenFounded(compilationPool.Tokens.Last(), compilationPool.FileName, line); continue; } if (IdentifierParser.IsIndentifier(compilationPool)) { OnTokenFounded(compilationPool.Tokens.Last(), compilationPool.FileName, line); continue; } Errors.Add(string.Format(Resources.Messages.UnexpectedSymbol, compilationPool.FileName, GetNextPartOfLexem(compilationPool), line)); return(false); } DetermineIdentifierTypes(compilationPool); compilationPool.Identifiers.ForEach(identifier => { Messages.Add(string.Format(Resources.Messages.IndentifierFounded, compilationPool.FileName, identifier.Type, identifier.Identity)); }); return(true); }
private void DetermineIdentifierTypes(CompilationPool compilationPool) { var rules = Constraints.Instance.Identifiers.CoreRules; var idenfitierTokens = compilationPool.Tokens .Where(token => token.Class == TokenClass.Identifier && compilationPool.Identifiers[token.Id].Type == 0) .GroupBy(token => token.Id) .Select(tokens => tokens.First()) .Select(token => new { Index = compilationPool.Tokens.IndexOf(token), Token = token }).ToList(); idenfitierTokens.ForEach(data => { foreach (var identifierRule in rules) { var isValid = true; foreach (var rule in identifierRule.Rules) { var index = data.Index + rule.Item1; if (index < 0 || index >= compilationPool.Tokens.Count) { continue; } var token = compilationPool.Tokens[index]; if (token.Class != rule.Item2 || token.Id != rule.Item3) { isValid = false; } } if (!isValid) { continue; } compilationPool.Identifiers[data.Token.Id].Type = identifierRule.IdentifierType; break; } }); }
public static string GetNextPartOfLexem(CompilationPool compilationPool) { var tokenClass = StateMachine.DetermineTokenClass(compilationPool.Code[compilationPool.CodePosition].ToString()); if (!tokenClass.HasValue) { throw new Exception("Token class not determined."); } var codePositionBackup = compilationPool.CodePosition; var count = 1; while (StateMachine.HasNextSymbol(compilationPool, tokenClass.Value)) { count++; compilationPool.CodePosition++; } var str = compilationPool.Code.Substring(codePositionBackup, count); compilationPool.CodePosition = codePositionBackup; return(str); }
public abstract bool TryBypass(CompilationPool dataPool);
public override bool TryBypass(CompilationPool dataPool) { throw new NotImplementedException(); }
public override bool TryBypass(CompilationPool compilationPool) { _pool = compilationPool; var table = Constraints.Instance.Tables.MainTable; var currentStateIndex = 0; var currentTokenIndex = 0; var stack = new Stack <int>(); do { var state = table.ElementAt(currentStateIndex); if (IsTokenExpected(table.ElementAt(currentStateIndex), currentTokenIndex)) { if (state.PushToStack.HasValue) { stack.Push(state.PushToStack.Value); } if (state.TransisionStateNumber == -1 && state.IsPopFromStackRequired == false) { Messages.Add(string.Format(Resources.Messages.ParsingSuccessful, _pool.FileName)); return(true); } if (state.IsAcceptRequired) { currentTokenIndex++; } // try // { currentStateIndex = state.IsPopFromStackRequired ? stack.Pop() : state.TransisionStateNumber; // } // catch (InvalidOperationException) // { // TODO // Errors.Add("Pop from stack"); // return false; // } } else if (state.IsErrorOccured) { var expected = state.ExpectedTokens.FirstOrDefault(); Errors.Add(string.Format(Resources.Messages.ParsingError, _pool.FileName, state.ExpectedTokens.Any() ? Constraints.Instance.Tokens.ToString(expected.Key, expected.Value) : "-", Constraints.Instance.Tokens.ToString(_pool.Tokens[currentTokenIndex].Class, _pool.Tokens[currentTokenIndex].Id))); // Errors.Add( // $"Syntax error : {_pool.Tokens[currentTokenIndex].Class}, {_pool.Tokens[currentTokenIndex].Id}, {_pool.Tokens[currentTokenIndex].Value} : {currentTokenIndex}. Expected: {string.Join("\n", state.ExpectedTokens.Select(pair => $"{pair.Key} : {pair.Value}"))}"); return(false); } else { currentStateIndex++; } } while (currentStateIndex != -1); // Messages.Add("Syntax analyzer: success."); return(true); }