diff --git a/CodeCompletion/DomSyntaxTreeVisitor.cs b/CodeCompletion/DomSyntaxTreeVisitor.cs index d1f15e1c8..79149434c 100644 --- a/CodeCompletion/DomSyntaxTreeVisitor.cs +++ b/CodeCompletion/DomSyntaxTreeVisitor.cs @@ -1383,6 +1383,7 @@ namespace CodeCompletion if (_function_header.return_type != null) _function_header.return_type.visit(this); return_type = returned_scope as TypeScope; + cur_scope = tmp; } } diff --git a/CodeCompletion/SymTable.cs b/CodeCompletion/SymTable.cs index 2d2d35f2a..32bf2660d 100644 --- a/CodeCompletion/SymTable.cs +++ b/CodeCompletion/SymTable.cs @@ -1844,6 +1844,7 @@ namespace CodeCompletion int i = 0; foreach (ElementScope parameter in this.parameters) { + i++; if (parameter.sc is UnknownScope || (parameter.sc is TypeScope) && (parameter.sc as TypeScope).IsGenericParameter) { int ind = this.template_parameters.IndexOf((parameter.sc as TypeScope).Name); @@ -1863,6 +1864,8 @@ namespace CodeCompletion inst_param = new ElementScope(parameter.si, parameter.sc, parameter.topScope); instance.parameters.Add(inst_param); } + if (parameter.param_kind == parametr_kind.params_parametr && i < gen_args.Count) + gen_args.RemoveRange(i, gen_args.Count - i); } instance.si = this.si; instance.return_type = this.return_type.GetInstance(gen_args); @@ -2517,6 +2520,13 @@ namespace CodeCompletion si.describe = name + " in " + declScope.si.name; } + public override TypeScope GetInstance(List gen_args) + { + if (gen_args.Count > 0) + return gen_args[0]; + return this; + } + public override bool IsGenericParameter { get @@ -3855,6 +3865,8 @@ namespace CodeCompletion if (this is UnknownScope && ts is CompiledScope && (ts as CompiledScope).CompiledType.IsGenericParameter || ts is UnknownScope && this is CompiledScope && (this as CompiledScope).CompiledType.IsGenericParameter) return true; + if (this.IsGenericParameter && ts.IsGenericParameter && this.Name == ts.Name) + return true; TypeScope tmp = this.baseScope; while (tmp != null) if (tmp.IsEqual(ts)) @@ -4767,10 +4779,19 @@ namespace CodeCompletion { Type t = this.ctn; if (!ctn.IsGenericTypeDefinition) - t = PascalABCCompiler.NetHelper.NetHelper.FindType(this.ctn.Namespace+"."+this.ctn.Name); + t = PascalABCCompiler.NetHelper.NetHelper.FindType(this.ctn.Namespace + "." + this.ctn.Name); + else if (this.instances != null && this.instances.Count > 0) + { + if (this.instances.Count != ctn.GetGenericArguments().Length) + { + Type t2 = PascalABCCompiler.NetHelper.NetHelper.FindType(this.ctn.Namespace + "." + this.ctn.Name.Substring(0, this.ctn.Name.IndexOf('`')) + "`" + this.instances.Count); + if (t2 != null) + t = t2; + } + } else if (gen_args.Count != ctn.GetGenericArguments().Length) - { - Type t2 = PascalABCCompiler.NetHelper.NetHelper.FindType(this.ctn.Namespace+"."+this.ctn.Name.Substring(0, this.ctn.Name.IndexOf('`')) + "`" + gen_args.Count); + { + Type t2 = PascalABCCompiler.NetHelper.NetHelper.FindType(this.ctn.Namespace + "." + this.ctn.Name.Substring(0, this.ctn.Name.IndexOf('`')) + "`" + gen_args.Count); if (t2 != null) t = t2; } @@ -4778,11 +4799,24 @@ namespace CodeCompletion sc.generic_params = new List(); sc.instances = new List(); sc.original_type = this; - for (int i = 0; i < gen_args.Count; i++) - { - sc.generic_params.Add(gen_args[i].si.name); - sc.instances.Add(gen_args[i]); - } + if (this.instances != null && this.instances.Count > 0) + for (int i = 0; i < this.instances.Count; i++) + { + if (this.instances[i] is UnknownScope || this.instances[i] is TemplateParameterScope) + { + List lst = new List(); + lst.Add(gen_args[Math.Min(i, gen_args.Count - 1)]); + sc.instances.Add(this.instances[i].GetInstance(lst)); + } + else + sc.instances.Add(this.instances[i].GetInstance(gen_args)); + } + else + for (int i = 0; i < gen_args.Count; i++) + { + sc.generic_params.Add(gen_args[i].si.name); + sc.instances.Add(gen_args[i]); + } sc.si.describe = sc.GetDescription(); return sc; } @@ -6275,7 +6309,7 @@ namespace CodeCompletion generic_args = new List(); generic_args.AddRange(args); } - if (generic_args != null) + if (generic_args != null || declaringType.instances != null ) { //TypeScope ts = declaringType.instances[pi.PropertyType.GenericParameterPosition]; this.sc = CompiledScope.get_type_instance(pi.PropertyType, declaringType.instances); @@ -6911,6 +6945,8 @@ namespace CodeCompletion public override TypeScope GetInstance(List gen_args) { + if (gen_args.Count > 0) + return gen_args[0]; return this; } diff --git a/NETGenerator/NETGenerator.cs b/NETGenerator/NETGenerator.cs index 8c2fff500..9b4815f71 100644 --- a/NETGenerator/NETGenerator.cs +++ b/NETGenerator/NETGenerator.cs @@ -2260,7 +2260,11 @@ namespace PascalABCCompiler.NETGenerator if (parameters[i].is_params) pb.SetCustomAttribute(TypeFactory.ParamArrayAttributeConstructor, new byte[] { 0x1, 0x0, 0x0, 0x0 }); if (default_value != null) + { + if (default_value.GetType() != param_types[i + num] && param_types[i + num].IsEnum && (Environment.OSVersion.Platform == PlatformID.Unix || Environment.OSVersion.Platform == PlatformID.MacOSX)) + default_value = Enum.ToObject(param_types[i + num], default_value); pb.SetConstant(default_value); + } if (func.functions_nodes.Length > 0) { FieldBuilder fb = null; diff --git a/ParserTools/ParserTools.csproj b/ParserTools/ParserTools.csproj index 91744a9ce..49dba70aa 100644 --- a/ParserTools/ParserTools.csproj +++ b/ParserTools/ParserTools.csproj @@ -70,20 +70,6 @@ - - - - - - - - - - - - - - diff --git a/ParserTools/ParserTools/Errors.cs b/ParserTools/ParserTools/Errors.cs index 7f2d87001..1ed1fa0de 100644 --- a/ParserTools/ParserTools/Errors.cs +++ b/ParserTools/ParserTools/Errors.cs @@ -46,16 +46,6 @@ namespace PascalABCCompiler.Errors { } } - public class TokenReadError : SyntaxError - { - public TokenReadError(PascalABCCompiler.ParserTools.GPBParser parser) - : base(string.Format(ParserErrorsStringResources.Get("UNEXPECTED_SYMBOL{0}"), parser.LRParser.TokenText!="" ? parser.LRParser.TokenText : "(EOF)"), - parser.current_file_name, - parser.parsertools.GetTokenSourceContext(parser.LRParser), - (syntax_tree_node)parser.prev_node) - { - } - } public class BadExpr : SyntaxError { public BadExpr(string _file_name, SourceContext _source_context, syntax_tree_node _node) @@ -63,17 +53,6 @@ namespace PascalABCCompiler.Errors { } } - - - - public class UnexpectedToken : SyntaxError - { - public UnexpectedToken(PascalABCCompiler.ParserTools.GPBParser parser,string ExpectedTokens) - : base(string.Format(ParserErrorsStringResources.Get("EXPECTED{0}"), ExpectedTokens), - parser.current_file_name, parser.parsertools.GetTokenSourceContext(parser.LRParser), (syntax_tree_node)parser.prev_node) - { - } - } public class TooBigCharNumberInSharpCharConstant: SyntaxError { public TooBigCharNumberInSharpCharConstant(string _file_name, SourceContext _source_context, syntax_tree_node _node) diff --git a/ParserTools/ParserTools/GPBParserTools.cs b/ParserTools/ParserTools/GPBParserTools.cs deleted file mode 100644 index 94effb695..000000000 --- a/ParserTools/ParserTools/GPBParserTools.cs +++ /dev/null @@ -1,433 +0,0 @@ -// Copyright (c) Ivan Bondarev, Stanislav Mihalkovich (for details please see \doc\copyright.txt) -// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) -using System; -using System.Text; -using System.Collections.Generic; -//using com.calitha.goldparser; -using System.Runtime.Serialization; -using PascalABCCompiler.SyntaxTree; -using PascalABCCompiler.Errors; -using System.IO; -using GoldParser; - -namespace PascalABCCompiler.ParserTools -{ - public class GPBParser // Странный класс, не являющийся парсером, а служащий для передачи информации. Состоит из двух частей: ссылок на парсер и грамматику и информации для передачи - { - public Parser LRParser; - public Grammar LanguageGrammar; - public List errors; - public string current_file_name; - public int max_errors = 10; - public object prev_node = null; - public parser_tools parsertools; - public List CompilerDirectives; - - public GPBParser(Stream stream) - { - LRParser = null; - LanguageGrammar = new GoldParser.Grammar(new BinaryReader(stream)); - } - - public GPBParser(GoldParser.Grammar grammar) - { - LRParser = null; - LanguageGrammar = grammar; - } - - public GPBParser() - { - LRParser = null; - LanguageGrammar = null; - } - } - - [Serializable()] - public class SymbolException : System.Exception - { - public SymbolException(string message) : base(message) - { - } - - public SymbolException(string message, - Exception inner) : base(message, inner) - { - } - - protected SymbolException(SerializationInfo info, - StreamingContext context) : base(info, context) - { - } - - } - - [Serializable()] - public class RuleException : System.Exception - { - - public RuleException(string message) : base(message) - { - } - - public RuleException(string message, - Exception inner) : base(message, inner) - { - } - - protected RuleException(SerializationInfo info, - StreamingContext context) : base(info, context) - { - } - - } - public class parser_tools - { - private const int max_char_const=0xFFFF; - - public int LineCorrection = 0; - public GoldParser.Parser parser; // Ужас - два разных парсера! SSM 19.01.12 - - public virtual void check_comment_text(GPBParser parser) - { - - } - - public virtual SourceContext GetTokenSourceContext() - { - int LineNum = parser.TokenLineNumber + LineCorrection; - return new SourceContext(LineNum, parser.TokenLinePosition, LineNum, parser.TokenLinePosition + parser.TokenLength - 1, parser.TokenCharPosition, parser.TokenCharPosition + parser.TokenLength); - } - - public virtual SourceContext GetTokenSourceContext(GoldParser.Parser parser) - { - int LineNum = parser.TokenLineNumber + LineCorrection; - return new SourceContext(LineNum, parser.TokenLinePosition, LineNum, parser.TokenLinePosition + parser.TokenLength - 1, parser.TokenCharPosition, parser.TokenCharPosition + parser.TokenLength); - } - - /*public virtual string terminal_token_to_string(TerminalToken token) - { - string name=symbol_to_string(token.Symbol); - if (name!=null) return name; - if (token.UserObject!=null) - { - if (token.UserObject is token_info) return ((token_info)token.UserObject).text; - return token.UserObject.ToString(); - } - - return token.Text; - } */ - - public virtual List GetPrioritySymbols(Symbol[] Symbols) - { - List res = new List(); - int maxp = -1, p; - foreach (Symbol symbol in Symbols) - { - p = symbol_priority(symbol); - if (p > maxp) maxp = p; - } - foreach (Symbol symbol in Symbols) - if (symbol_priority(symbol) == maxp) - res.Add(symbol); - return res; - } - - public string[] SymbolsToStrings(Symbol[] Symbols) - { - string[] res = new string[Symbols.Length>0?1:0]; - for (int i = 0; i < res.Length; i++) - res[i] = symbol_to_string(Symbols[i]); - return res; - } - - public virtual string symbol_collection_to_string(Symbol[] Symbols) - { - return PascalABCCompiler.FormatTools.ObjectsToString(SymbolsToStrings(GetPrioritySymbols(Symbols).ToArray()), ","); - } - - public virtual string symbol_to_string(Symbol symbol) - { - return null; - } - - public virtual int symbol_priority(Symbol symbol) - { - return 0; - } - - public virtual ident create_directive_name(GPBParser parser) - { - return create_directive_name(parser.LRParser.TokenString, GetTokenSourceContext(parser.LRParser)); - } - - public virtual ident create_directive_name(string text, SourceContext sc) - { - ident dn = new ident(new string(text.ToCharArray(1, text.Length - 1))); - dn.source_context = sc; - return dn; - } - - public virtual string ReplaceSpecialSymbols(string text) - { - text = text.Replace("''", "'"); - return text; - } - - public virtual char_const create_char_const(GPBParser parser) - { - return create_char_const(parser.LRParser.TokenString,GetTokenSourceContext(parser.LRParser)); - } - - public char_const create_char_const(string text, SourceContext sc) - { - string char_text = new string(text.ToCharArray(1, text.Length - 2)); - char_text = ReplaceSpecialSymbols(char_text); - char_const ct = new char_const(); - ct.source_context = sc; - if (char_text.Length == 1) - { - ct.cconst = char_text[0]; - return ct; - } - return null; - } - - public virtual sharp_char_const create_sharp_char_const(GPBParser parser) - { - string text = parser.LRParser.TokenString; - string int_text = new string(text.ToCharArray(1, text.Length - 1)); - SourceContext sc = GetTokenSourceContext(parser.LRParser);//create_source_context(token); - sharp_char_const scc = null; - int val = 0; - if (int.TryParse(int_text, out val)) - { - if (val > max_char_const) - { - scc = new sharp_char_const(0); - parser.errors.Add(new TooBigCharNumberInSharpCharConstant(parser.current_file_name, sc, scc)); - } - else - scc = new sharp_char_const(val); - scc.source_context = sc; - } - else - { - parser.errors.Add(new TooBigCharNumberInSharpCharConstant(parser.current_file_name, sc, scc)); - } - return scc; - } - - public virtual const_node create_double_const(GPBParser parser) - { - const_node cn=null; - SourceContext sc = GetTokenSourceContext(parser.LRParser); - try - { - System.Globalization.NumberFormatInfo sgnfi=new System.Globalization.NumberFormatInfo(); - sgnfi.NumberDecimalSeparator="."; - double val=double.Parse(parser.LRParser.TokenString,sgnfi); - cn=new double_const(val); - cn.source_context=sc; - } - catch(Exception) - { - parser.errors.Add(new BadFloat(parser.current_file_name,sc,(syntax_tree_node)parser.prev_node)); - } - return cn; - } - - public virtual const_node create_int_const(GPBParser parser) - { - return create_int_const(parser, System.Globalization.NumberStyles.Integer); - } - - public virtual const_node create_hex_const(GPBParser parser) - { - return create_int_const(parser, System.Globalization.NumberStyles.HexNumber); - } - - public virtual const_node create_int_const(GPBParser parser, System.Globalization.NumberStyles NumberStyles) - { - //таблица целых констант на уровне синтаксиса - // не может быть - 0 + - // 32--------16----8----|----8----16--------32----------------64(bits) - // [ int64 )[ int32 ]( int64 ]( uint64 ] - string text = parser.LRParser.TokenString; - if (NumberStyles == System.Globalization.NumberStyles.HexNumber) - text = text.Substring(1); - const_node cn=new int32_const(); - SourceContext sc = GetTokenSourceContext(parser.LRParser);//create_source_context(token); - if (text.Length < 8) - (cn as int32_const).val = Int32.Parse(text, NumberStyles); - else - { - try - { - UInt64 uint64 = UInt64.Parse(text, NumberStyles); - if (uint64 <= Int32.MaxValue) - (cn as int32_const).val = (Int32)uint64; - else - if (uint64 <= Int64.MaxValue) - cn = new int64_const((Int64)uint64); - else - cn = new uint64_const(uint64); - } - catch (Exception) - { - if(NumberStyles == System.Globalization.NumberStyles.HexNumber) - parser.errors.Add(new BadHex(parser.current_file_name, sc, (syntax_tree_node)parser.prev_node)); - else - parser.errors.Add(new BadInt(parser.current_file_name, sc, (syntax_tree_node)parser.prev_node)); - } - } - cn.source_context = sc; - return cn; - } - - public virtual ident create_ident(GPBParser parser) - { - string text = parser.LRParser.TokenString; - if (text.IndexOf('&') == 0) - text = text.Substring(1); - ident id = new ident(text); - id.source_context = GetTokenSourceContext(parser.LRParser); - return id; - } - - public procedure_attributes_list AddModifier(procedure_attributes_list proc_list, SyntaxTree.proc_attribute modif) - { - if (proc_list == null) - proc_list = new procedure_attributes_list(); - foreach (procedure_attribute attr in proc_list.proc_attributes) - if (attr.attribute_type == modif) - return proc_list; - proc_list.proc_attributes.Add(new procedure_attribute(modif)); - return proc_list; - } - - public virtual literal create_string_const(GPBParser parser) - { - literal lt; - string text = parser.LRParser.TokenString; - if (text.Length==3 && text[0]=='\'' && text[2]=='\'') - { - lt=new char_const(text[1]); - lt.source_context = GetTokenSourceContext(parser.LRParser);//create_source_context(token); - return lt; - } - text = ReplaceSpecialSymbols(text.Substring(1, text.Length - 2)); - lt=new string_const(text); - lt.source_context = GetTokenSourceContext(parser.LRParser);//create_source_context(token); - return lt; - - } - - /*public SourceContext create_source_context(com.calitha.goldparser.TerminalToken token) - { - return new SourceContext(token.Location.LineNr+1,token.Location.ColumnNr+1,token.Location.LineNr+1,token.Location.ColumnNr+token.Text.Length); - } - public file_position create_file_position(com.calitha.goldparser.TerminalToken token) - { - return new file_position(token.Location.LineNr+1,token.Location.ColumnNr+1); - }*/ - - public void create_source_context(object to,object left,object right) - { - if (to != null) - ((syntax_tree_node)to).source_context = get_source_context(left, right); - } - - public SourceContext get_source_context(object left, object right) - { - //debug - /*if (left == null && right!=null) - { - Console.WriteLine("\n\rerror: left is null(create_source_context)!\n\r"); - Console.WriteLine(((syntax_tree_node)right).source_context.ToString()); - } - if (right == null && left!=null) - { - Console.WriteLine("\n\rerror: right is null(create_source_context)!\n\r"); - Console.WriteLine(((syntax_tree_node)left).source_context.ToString()); - } - if (((syntax_tree_node)left).source_context == null) - { - Console.WriteLine("\n\rerror: source_context is null!(left)\n\r"); - return null; - } - if (((syntax_tree_node)right).source_context == null) - { - Console.WriteLine("\n\rerror: source_context is null!(right)\n\r"); - return null; - } - */ - if ((left == null) || (right == null) || (((syntax_tree_node)left).source_context == null) || (((syntax_tree_node)right).source_context == null)) - return null; - return new SourceContext(((syntax_tree_node)left).source_context, ((syntax_tree_node)right).source_context); - } - - public void create_source_context_left(object to, object left) - { - file_position fp=((syntax_tree_node)left).source_context.begin_position; - ((syntax_tree_node)to).source_context = new SourceContext(fp.line_num,fp.column_num,fp.line_num,fp.column_num,0,0); - } - - public void create_source_context_right(object to, object right) - { - file_position fp = ((syntax_tree_node)right).source_context.end_position; - ((syntax_tree_node)to).source_context = new SourceContext(fp.line_num, fp.column_num, fp.line_num, fp.column_num, 0, 0); - } - - public object sc_not_null(object o1, object o2) - { - if (o1 != null) - if (((syntax_tree_node)o1).source_context != null) return o1; - return o2; - } - - public object sc_not_null(object o1, object o2,object o3) - { - if (o1 != null) - if (((syntax_tree_node)o1).source_context != null) return o1; - if (o2 != null) - if (((syntax_tree_node)o2).source_context != null) return o2; - return o3; - } - - public object sc_not_null(params object[] arr) - { - foreach(object o in arr) - if (o!=null) - if (((syntax_tree_node)o).source_context!=null) return o; - return null; - } - - public void assign_source_context(object to,object from) - { - //debug - //if (((tree_node)from).source_context==null) Console.WriteLine("\n\rerror: from sc is null(assign_source_context)!\n\r"); - ((syntax_tree_node)to).source_context=((syntax_tree_node)from).source_context; - } - - /*public SourceContext create_source_context(Token[] tokens) - { - object right=null,left=null; - for(int i=0;i - /// State in the Deterministic Finite Automata - /// which is used by the tokenizer. - /// - public class DfaState - { - private int m_index; - internal Symbol m_acceptSymbol; - internal ObjectMap m_transitionVector; - - /// - /// Creates a new instance of the DfaState class. - /// - /// Index in the DFA state table. - /// Symbol to accept. - /// Transition vector. - public DfaState(int index, Symbol acceptSymbol, ObjectMap transitionVector) - { - m_index = index; - m_acceptSymbol = acceptSymbol; - m_transitionVector = transitionVector; - } - - /// - /// Gets index of the state in DFA state table. - /// - public int Index - { - get { return m_index; } - } - - /// - /// Gets the symbol which can be accepted in this DFA state. - /// - public Symbol AcceptSymbol - { - get { return m_acceptSymbol; } - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/Grammar.cs b/ParserTools/ParserTools/GoldParser/Grammar.cs deleted file mode 100644 index c6edeb07f..000000000 --- a/ParserTools/ParserTools/GoldParser/Grammar.cs +++ /dev/null @@ -1,624 +0,0 @@ -//#define STRONG_CGT_CHECK - -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; -using System.IO; -using System.Text; -using System.Collections; - -#endregion - -namespace GoldParser -{ - /// - /// Contains grammar tables required for parsing. - /// - public class Grammar - { - #region Fields and constants - - /// - /// Identifies Gold parser grammar file. - /// - public const string FileHeader = "GOLD Parser Tables/v1.0"; - - // Grammar header information - private string m_name; // Name of the grammar - private string m_version; // Version of the grammar - private string m_author; // Author of the grammar - private string m_about; // Grammar description - private int m_startSymbolIndex; // Start symbol index - private bool m_caseSensitive; // Grammar is case sensitive or not - - // Tables read from the binary grammar file - internal Symbol[] m_symbolTable; // Symbol table - private String[] m_charSetTable; // Charset table - internal Rule[] m_ruleTable; // Rule table - internal DfaState[] m_dfaStateTable; // DFA state table - internal LRState[] m_lrStateTable; // LR state table - - // Initial states - internal int m_dfaInitialStateIndex; // DFA initial state index - internal DfaState m_dfaInitialState; // DFA initial state - internal int m_lrInitialState; // LR initial state - - // Internal state of grammar parser - private BinaryReader m_reader; // Source of the grammar - private int m_entryCount; // Number of entries left - - internal Symbol m_errorSymbol; - internal Symbol m_endSymbol; - - #endregion - - #region Constructors - - /// - /// Creates a new instance of Grammar class - /// - /// - public Grammar(BinaryReader reader) - { - if (reader == null) - { - throw new ArgumentNullException("reader"); - } - - m_reader = reader; - Load(); - } - - #endregion - - #region Public members - - /// - /// Gets grammar name. - /// - public string Name - { - get { return m_name; } - } - - /// - /// Gets grammar version. - /// - public string Version - { - get { return m_version; } - } - - /// - /// Gets grammar author. - /// - public string Author - { - get { return m_author; } - } - - /// - /// Gets grammar description. - /// - public string About - { - get { return m_about; } - } - - /// - /// Gets the start symbol for the grammar. - /// - public Symbol StartSymbol - { - get { return m_symbolTable[m_startSymbolIndex]; } - } - - /// - /// Gets the value indicating if the grammar is case sensitive. - /// - public bool CaseSensitive - { - get { return m_caseSensitive; } - } - - /// - /// Gets initial DFA state. - /// - public DfaState DfaInitialState - { - get { return m_dfaInitialState; } - } - - /// - /// Gets initial LR state. - /// - public LRState InitialLRState - { - get { return m_lrStateTable[m_lrInitialState]; } - } - - /// - /// Gets a special symbol to designate last token in the input stream. - /// - public Symbol EndSymbol - { - get { return m_endSymbol; } - } - - #endregion - - #region Private members - - /// - /// Loads grammar from the binary reader. - /// - private void Load() - { - if (FileHeader != ReadString()) - { - throw new FileLoadException(SR.GetString(SR.Grammar_WrongFileHeader)); - } - while (m_reader.PeekChar() != -1) - { - RecordType recordType = ReadNextRecord(); - switch (recordType) - { - case RecordType.Parameters: - ReadHeader(); - break; - - case RecordType.TableCounts: - ReadTableCounts(); - break; - - case RecordType.Initial: - ReadInitialStates(); - break; - - case RecordType.Symbols: - ReadSymbols(); - break; - - case RecordType.CharSets: - ReadCharSets(); - break; - - case RecordType.Rules: - ReadRules(); - break; - - case RecordType.DfaStates: - ReadDfaStates(); - break; - - case RecordType.LRStates: - ReadLRStates(); - break; - - default: - throw new FileLoadException(SR.GetString(SR.Grammar_InvalidRecordType)); - } - } - m_dfaInitialState = m_dfaStateTable[m_dfaInitialStateIndex]; - OptimizeDfaTransitionVectors(); - } - - /// - /// Reads the next record in the binary grammar file. - /// - /// Read record type. - private RecordType ReadNextRecord() - { - char recordType = (char) ReadByte(); - //Structure below is ready for future expansion - switch (recordType) - { - case 'M': - //Read the number of entry's - m_entryCount = ReadInt16(); - return (RecordType) ReadByteEntry(); - - default: - throw new FileLoadException(SR.GetString(SR.Grammar_InvalidRecordHeader)); - } - } - - /// - /// Reads grammar header information. - /// - private void ReadHeader() - { - m_name = ReadStringEntry(); - m_version = ReadStringEntry(); - m_author = ReadStringEntry(); - m_about = ReadStringEntry(); - m_caseSensitive = ReadBoolEntry(); - m_startSymbolIndex = ReadInt16Entry(); - } - - /// - /// Reads table record counts and initializes tables. - /// - private void ReadTableCounts() - { - // Initialize tables - m_symbolTable = new Symbol [ReadInt16Entry()]; - m_charSetTable = new String [ReadInt16Entry()]; - m_ruleTable = new Rule [ReadInt16Entry()]; - m_dfaStateTable = new DfaState [ReadInt16Entry()]; - m_lrStateTable = new LRState [ReadInt16Entry()]; - } - - /// - /// Read initial DFA and LR states. - /// - private void ReadInitialStates() - { - m_dfaInitialStateIndex = ReadInt16Entry(); - m_lrInitialState = ReadInt16Entry(); - } - - /// - /// Read symbol information. - /// - private void ReadSymbols() - { - int index = ReadInt16Entry(); - string name = ReadStringEntry(); - SymbolType symbolType = (SymbolType) ReadInt16Entry(); - - Symbol symbol = new Symbol(index, name, symbolType); - switch (symbolType) - { - case SymbolType.Error: - m_errorSymbol = symbol; - break; - - case SymbolType.End: - m_endSymbol = symbol; - break; - } - m_symbolTable[index] = symbol; - } - - /// - /// Read char set information. - /// - private void ReadCharSets() - { - m_charSetTable[ReadInt16Entry()] = ReadStringEntry(); - } - - /// - /// Read rule information. - /// - private void ReadRules() - { - int index = ReadInt16Entry(); - Symbol nonTerminal = m_symbolTable[ReadInt16Entry()]; - ReadEmptyEntry(); - Symbol[] symbols = new Symbol[m_entryCount]; - for (int i = 0 ; i < symbols.Length; i++) - { - symbols[i] = m_symbolTable[ReadInt16Entry()]; - } - Rule rule = new Rule(index, nonTerminal, symbols); - m_ruleTable[index] = rule; - } - - /// - /// Read DFA state information. - /// - private void ReadDfaStates() - { - int index = ReadInt16Entry(); - Symbol acceptSymbol = null; - bool acceptState = ReadBoolEntry(); - if (acceptState) - { - acceptSymbol = m_symbolTable[ReadInt16Entry()]; - } - else - { - ReadInt16Entry(); // Skip the entry. - } - ReadEmptyEntry(); - - // Read DFA edges - DfaEdge[] edges = new DfaEdge[m_entryCount / 3]; - for (int i = 0; i < edges.Length; i++) - { - edges[i].CharSetIndex = ReadInt16Entry(); - edges[i].TargetIndex = ReadInt16Entry(); - ReadEmptyEntry(); - } - - // Create DFA state and store it in DFA state table - ObjectMap transitionVector = CreateDfaTransitionVector(edges); - DfaState dfaState = new DfaState(index, acceptSymbol, transitionVector); - m_dfaStateTable[index] = dfaState; - } - - /// - /// Read LR state information. - /// - private void ReadLRStates() - { - int index = ReadInt16Entry(); - ReadEmptyEntry(); - LRStateAction[] stateTable = new LRStateAction[m_entryCount / 4]; - for (int i = 0; i < stateTable.Length; i++) - { - Symbol symbol = m_symbolTable[ReadInt16Entry()]; - LRAction action = (LRAction) ReadInt16Entry(); - int targetIndex = ReadInt16Entry(); - ReadEmptyEntry(); - stateTable[i] = new LRStateAction(i, symbol, action, targetIndex); - } - - // Create the transition vector - LRStateAction[] transitionVector = new LRStateAction[m_symbolTable.Length]; - for (int i = 0; i < transitionVector.Length; i++) - { - transitionVector[i] = null; - } - for (int i = 0; i < stateTable.Length; i++) - { - transitionVector[stateTable[i].Symbol.Index] = stateTable[i]; - } - - LRState lrState = new LRState(index, stateTable, transitionVector); - m_lrStateTable[index] = lrState; - } - - /// - /// Creates the DFA state transition vector. - /// - /// Array of automata edges. - /// Hashtable with the transition information. - private ObjectMap CreateDfaTransitionVector(DfaEdge[] edges) - { - ObjectMap transitionVector = new ObjectMap(); - for (int i = edges.Length; --i >= 0;) - { - string charSet = m_charSetTable[edges[i].CharSetIndex]; - for (int j = 0; j < charSet.Length; j++) - { - transitionVector[charSet[j]] = edges[i].TargetIndex; - } - } - return transitionVector; - } - - /// - /// Reads empty entry from the grammar file. - /// - private void ReadEmptyEntry() - { -#if STRONG_CGT_CHECK - if (ReadEntryType() != EntryType.Empty) - { - throw new FileLoadException(SR.GetString(SR.Grammar_EmptyEntryExpected)); - } -#elif !STRONG_CGT_CHECK - m_reader.ReadByte(); -#endif - m_entryCount--; - } - - /// - /// Reads string entry from the grammar file. - /// - /// String entry content. - private string ReadStringEntry() - { -#if STRONG_CGT_CHECK - if (ReadEntryType() != EntryType.String) - { - throw new FileLoadException(SR.GetString(SR.Grammar_StringEntryExpected)); - } -#elif !STRONG_CGT_CHECK - m_reader.ReadByte(); -#endif - m_entryCount--; - return ReadString(); - } - - /// - /// Reads Int16 entry from the grammar file. - /// - /// Int16 entry content. - private int ReadInt16Entry() - { -#if STRONG_CGT_CHECK - if (ReadEntryType() != EntryType.Integer) - { - throw new FileLoadException(SR.GetString(SR.Grammar_IntegerEntryExpected)); - } -#elif !STRONG_CGT_CHECK - m_reader.ReadByte(); -#endif - m_entryCount--; - return ReadInt16(); - } - - /// - /// Reads byte entry from the grammar file. - /// - /// Byte entry content. - private byte ReadByteEntry() - { -#if STRONG_CGT_CHECK - if (ReadEntryType() != EntryType.Byte) - { - throw new FileLoadException(SR.GetString(SR.Grammar_ByteEntryExpected)); - } -#elif !STRONG_CGT_CHECK - m_reader.ReadByte(); -#endif - m_entryCount--; - return ReadByte(); - } - - /// - /// Reads boolean entry from the grammar file. - /// - /// Boolean entry content. - private bool ReadBoolEntry() - { -#if STRONG_CGT_CHECK - if (ReadEntryType() != EntryType.Boolean) - { - throw new FileLoadException(SR.GetString(SR.Grammar_BooleanEntryExpected)); - } -#elif !STRONG_CGT_CHECK - m_reader.ReadByte(); -#endif - m_entryCount--; - return ReadBool(); - } - - /// - /// Reads entry type. - /// - /// Entry type. - private EntryType ReadEntryType() - { - if (m_entryCount == 0) - { - throw new FileLoadException(SR.GetString(SR.Grammar_NoEntry)); - } - return (EntryType) ReadByte(); - } - - /// - /// Reads string from the grammar file. - /// - /// String value. - private string ReadString() - { - StringBuilder result = new StringBuilder(); - char unicodeChar = (char) ReadInt16(); - while (unicodeChar != (char) 0) - { - result.Append(unicodeChar); - unicodeChar = (char) ReadInt16(); - } - return result.ToString(); - } - - /// - /// Reads two byte integer Int16 from the grammar file. - /// - /// Int16 value. - private int ReadInt16() - { - return m_reader.ReadUInt16(); - } - - /// - /// Reads byte from the grammar file. - /// - /// Byte value. - private byte ReadByte() - { - return m_reader.ReadByte(); - } - - /// - /// Reads boolean from the grammar file. - /// - /// Boolean value. - private bool ReadBool() - { - return (ReadByte() == 1); - } - - private void OptimizeDfaTransitionVectors() - { - DfaState[] dfaStates = m_dfaStateTable; - foreach (DfaState state in dfaStates) - { - ObjectMap transitions = state.m_transitionVector; - for (int i = transitions.Count; --i >= 0;) - { - int key = transitions.GetKey(i); - object transition = transitions[key]; - if (transition != null) - { - int transitionIndex = (int) transition; - if (transitionIndex >= 0) - { - transitions[key] = dfaStates[transitionIndex]; - } - else - { - transitions[key] = null; - } - } - } - transitions.ReadOnly = true; - } - } - - #endregion - - #region Private type definitions - - /// - /// Record type byte in the binary grammar file. - /// - private enum RecordType - { - Parameters = (int) 'P', // 80 - TableCounts = (int) 'T', // 84 - Initial = (int) 'I', // 73 - Symbols = (int) 'S', // 83 - CharSets = (int) 'C', // 67 - Rules = (int) 'R', // 82 - DfaStates = (int) 'D', // 68 - LRStates = (int) 'L', // 76 - Comment = (int) '!' // 33 - } - - /// - /// Entry type byte in the binary grammar file. - /// - private enum EntryType - { - Empty = (int) 'E', // 69 - Integer = (int) 'I', // 73 - String = (int) 'S', // 83 - Boolean = (int) 'B', // 66 - Byte = (int) 'b' // 98 - } - - /// - /// Edge between DFA states. - /// - private struct DfaEdge - { - public int CharSetIndex; - public int TargetIndex; - } - - #endregion - } -} diff --git a/ParserTools/ParserTools/GoldParser/LRAction.cs b/ParserTools/ParserTools/GoldParser/LRAction.cs deleted file mode 100644 index c0fb43e02..000000000 --- a/ParserTools/ParserTools/GoldParser/LRAction.cs +++ /dev/null @@ -1,61 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// LR parser action type. - /// - public enum LRAction - { - /// - /// No action. Not used. - /// - None = 0, - - /// - /// Shift a symbol and go to a state - /// - Shift = 1, - - /// - /// Reduce by a specified rule - /// - Reduce = 2, - - /// - /// Goto to a state on reduction - /// - Goto = 3, - - /// - /// Input successfully parsed - /// - Accept = 4, - - /// - /// Error - /// - Error = 5 - } -} diff --git a/ParserTools/ParserTools/GoldParser/LRState.cs b/ParserTools/ParserTools/GoldParser/LRState.cs deleted file mode 100644 index 153412f03..000000000 --- a/ParserTools/ParserTools/GoldParser/LRState.cs +++ /dev/null @@ -1,84 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// State of LR parser. - /// - public class LRState - { - private int m_index; - private LRStateAction[] m_actions; - internal LRStateAction[] m_transitionVector; - - /// - /// Creates a new instance of the LRState class - /// - /// Index of the LR state in the LR state table. - /// List of all available LR actions in this state. - /// Transition vector which has symbol index as an index. - public LRState(int index, LRStateAction[] actions, LRStateAction[] transitionVector) - { - m_index = index; - m_actions = actions; - m_transitionVector = transitionVector; - } - - /// - /// Gets index of the LR state in LR state table. - /// - public int Index - { - get { return m_index; } - } - - /// - /// Gets LR state action count. - /// - public int ActionCount - { - get { return m_actions.Length; } - } - - /// - /// Returns state action by its index. - /// - /// State action index. - /// LR state action for the given index. - public LRStateAction GetAction(int index) - { - return m_actions[index]; - } - - /// - /// Returns LR state action by symbol index. - /// - /// Symbol Index to search for. - /// LR state action object. - public LRStateAction GetActionBySymbolIndex(int symbolIndex) - { - return m_transitionVector[symbolIndex]; - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/LRStateAction.cs b/ParserTools/ParserTools/GoldParser/LRStateAction.cs deleted file mode 100644 index 3b7301ba6..000000000 --- a/ParserTools/ParserTools/GoldParser/LRStateAction.cs +++ /dev/null @@ -1,83 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// Action in a LR State. - /// - public class LRStateAction - { - private int m_index; - private Symbol m_symbol; - private LRAction m_action; - internal int m_value; - - /// - /// Creats a new instance of the LRStateAction class. - /// - /// Index of the LR state action. - /// Symbol associated with the action. - /// Action type. - /// Action value. - public LRStateAction(int index, Symbol symbol, LRAction action, int value) - { - m_index = index; - m_symbol = symbol; - m_action = action; - m_value = value; - } - - /// - /// Gets index of the LR state action. - /// - public int Index - { - get { return m_index; } - } - - /// - /// Gets symbol associated with the LR state action. - /// - public Symbol Symbol - { - get { return m_symbol; } - } - - /// - /// Gets action type. - /// - public LRAction Action - { - get { return m_action; } - } - - /// - /// Gets the action value. - /// - public int Value - { - get { return m_value; } - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/ObjectMap.cs b/ParserTools/ParserTools/GoldParser/ObjectMap.cs deleted file mode 100644 index 5b438bdb0..000000000 --- a/ParserTools/ParserTools/GoldParser/ObjectMap.cs +++ /dev/null @@ -1,448 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// Maps integer values used for transition vectors to objects. - /// - public class ObjectMap - { - #region Fields - - private bool m_readonly; - private MapProvider m_mapProvider; - - private const int MAXINDEX = 255; - private const int GROWTH = 32; - private const int MINSIZE = 32; - private const int MAXARRAYCOUNT = 12; - - private const int INVALIDKEY = Int32.MaxValue; - - #endregion - - #region Constructors - - /// - /// Creates new instance of class. - /// - public ObjectMap() - { - m_mapProvider = new SortedMapProvider(MINSIZE); - } - - #endregion - - #region Public members. - - /// - /// Gets number of entries in the map. - /// - public int Count - { - get { return m_mapProvider.m_count; } - } - - /// - /// Gets or sets read only flag. - /// - public bool ReadOnly - { - get { return m_readonly; } - set - { - if (m_readonly != value) - { - SetMapProvider(value); - m_readonly = value; - } - } - } - - /// - /// Gets or sets value by key. - /// - public object this[int key] - { - get { return m_mapProvider[key]; } - set { m_mapProvider.Add(key, value); } - } - - /// - /// Returns key by index. - /// - /// Zero based index of the requested key. - /// Returns key for the given index. - public int GetKey(int index) - { - return m_mapProvider.GetEntry(index).Key; - } - - /// - /// Removes entry by its key. - /// - /// - public void Remove(int key) - { - m_mapProvider.Remove(key); - } - - /// - /// Adds a new key and value pair. - /// If key exists then value is applied to existing key. - /// - /// New key to add. - /// Value for the key. - public void Add(int key, object value) - { - m_mapProvider.Add(key, value); - } - - #endregion - - #region Private members - - private void SetMapProvider(bool readOnly) - { - int count = m_mapProvider.m_count; - MapProvider provider = m_mapProvider; - if (readOnly) - { - SortedMapProvider pr = m_mapProvider as SortedMapProvider; - if (pr.m_lastKey <= MAXINDEX) - { - provider = new IndexMapProvider(); - } - else if (count <= MAXARRAYCOUNT) - { - provider = new ArrayMapProvider(m_mapProvider.m_count); - } - } - else - { - if (! (provider is SortedMapProvider)) - { - provider = new SortedMapProvider(m_mapProvider.m_count); - } - } - if (provider != m_mapProvider) - { - for (int i = 0; i < count; i++) - { - Entry entry = m_mapProvider.GetEntry(i); - provider.Add(entry.Key, entry.Value); - } - m_mapProvider = provider; - } - } - - #endregion - - #region Entry struct definition - - private struct Entry - { - internal int Key; - internal object Value; - - internal Entry(int key, object value) - { - Key = key; - Value = value; - } - } - - #endregion - - private abstract class MapProvider - { - internal int m_count; // Entry count in the collection. - - internal abstract object this[int key] - { - get; - } - - internal abstract Entry GetEntry(int index); - - internal abstract void Add(int key, object value); - - internal virtual void Remove(int key) - { - throw new InvalidOperationException(); - } - } - - private class SortedMapProvider : MapProvider - { - internal Entry[] m_entries; // Array of entries. - - internal int m_lastKey; // Bigest key number. - - internal SortedMapProvider(int capacity) - { - m_entries = new Entry[capacity]; - } - - internal override object this[int key] - { - get - { - int minIndex = 0; - int maxIndex = m_count - 1; - if (maxIndex >= 0 && key <= m_lastKey) - { - do - { - int midIndex = (maxIndex + minIndex) / 2; - if (key <= m_entries[midIndex].Key) - { - maxIndex = midIndex; - } - else - { - minIndex = midIndex + 1; - } - } while (minIndex < maxIndex); - if (key == m_entries[minIndex].Key) - { - return m_entries[minIndex].Value; - } - } - return null; - } - } - - internal override Entry GetEntry(int index) - { - return m_entries[index]; - } - - internal override void Add(int key, object value) - { - bool found; - int index = FindInsertIndex(key, out found); - if (found) - { - m_entries[index].Value = value; - return; - } - if (m_count >= m_entries.Length) - { - Entry[] entries = new Entry[m_entries.Length + GROWTH]; - Array.Copy(m_entries, 0, entries, 0, m_entries.Length); - m_entries = entries; - } - if (index < m_count) - { - Array.Copy(m_entries, index, m_entries, index + 1, m_count - index); - } - else - { - m_lastKey = key; - } - m_entries[index].Key = key; - m_entries[index].Value = value; - m_count++; - } - - internal override void Remove(int key) - { - int index = FindIndex(key); - if (index >= 0) - { - int tailSize = (m_count - 1) - index; - if (tailSize > 0) - { - Array.Copy(m_entries, index + 1, m_entries, index, tailSize); - } - else if (m_count > 1) - { - m_lastKey = m_entries[m_count - 2].Key; - } - else - { - m_lastKey = INVALIDKEY; - } - m_count--; - m_entries[m_count].Key = INVALIDKEY; - m_entries[m_count].Value = null; - } - } - - private int FindIndex(int key) - { - int minIndex = 0; - if (m_count > 0 && key <= m_lastKey) - { - int maxIndex = m_count - 1; - do - { - int midIndex = (maxIndex + minIndex) / 2; - if (key <= m_entries[midIndex].Key) - { - maxIndex = midIndex; - } - else - { - minIndex = midIndex + 1; - } - } while (minIndex < maxIndex); - if (key == m_entries[minIndex].Key) - { - return minIndex; - } - } - return -1; - } - - private int FindInsertIndex(int key, out bool found) - { - int minIndex = 0; - if (m_count > 0 && key <= m_lastKey) - { - int maxIndex = m_count - 1; - do - { - int midIndex = (maxIndex + minIndex) / 2; - if (key <= m_entries[midIndex].Key) - { - maxIndex = midIndex; - } - else - { - minIndex = midIndex + 1; - } - } while (minIndex < maxIndex); - found = (key == m_entries[minIndex].Key); - return minIndex; - } - found = false; - return m_count; - } - } - - private class IndexMapProvider : MapProvider - { - private object[] m_array; // Array of entries. - - internal IndexMapProvider() - { - m_array = new object[MAXINDEX + 1]; - for (int i = m_array.Length; --i >= 0; ) - { - m_array[i] = Unassigned.Value; - } - } - - internal override object this[int key] - { - get - { - if (key >= m_array.Length || key < 0) - { - return null; - } - return m_array[key]; - } - } - - internal override Entry GetEntry(int index) - { - int idx = -1; - for (int i = 0; i < m_array.Length; i++) - { - object value = m_array[i]; - if (value != Unassigned.Value) - { - idx++; - } - if (idx == index) - { - return new Entry(i, value); - } - } - return new Entry(); - } - - internal override void Add(int key, object value) - { - m_array[key] = value; - m_count++; - } - } - - private class ArrayMapProvider : MapProvider - { - private Entry[] m_entries; // Array of entries. - - internal ArrayMapProvider(int capacity) - { - m_entries = new Entry[capacity]; - } - - internal override object this[int key] - { - get - { - for (int i = m_count; --i >= 0;) - { - Entry entry = m_entries[i]; - int entryKey = entry.Key; - if (entryKey < key) - { - continue; - } - else if (entryKey == key) - { - return entry.Value; - } - else if (entryKey > key) - { - return null; - } - } - return null; - } - } - - internal override Entry GetEntry(int index) - { - return m_entries[index]; - } - - internal override void Add(int key, object value) - { - m_entries[m_count].Key = key; - m_entries[m_count].Value = value; - m_count++; - } - } - - private class Unassigned - { - internal static Unassigned Value = new Unassigned(); - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/ParseMessage.cs b/ParserTools/ParserTools/GoldParser/ParseMessage.cs deleted file mode 100644 index db4ec33e8..000000000 --- a/ParserTools/ParserTools/GoldParser/ParseMessage.cs +++ /dev/null @@ -1,108 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// Available parse messages. - /// - public enum ParseMessage - { - /// - /// Nothing - /// - Empty = 0, - - /// - /// Each time a token is read, this message is generated. - /// - TokenRead = 1, - - /// - /// When the engine is able to reduce a rule, - /// this message is returned. The rule that was - /// reduced is set in the GOLDParser's ReduceRule property. - /// The tokens that are reduced and correspond the - /// rule's definition are stored in the Tokens() property. - /// - Reduction = 2, - - /// - /// The engine will returns this message when the source - /// text has been accepted as both complete and correct. - /// In other words, the source text was successfully analyzed. - /// - Accept = 3, - - /// - /// Before any parsing can take place, - /// a Compiled Grammar Table file must be loaded. - /// - NotLoadedError = 4, - - /// - /// The tokenizer will generate this message when - /// it is unable to recognize a series of characters - /// as a valid token. To recover, pop the invalid - /// token from the input queue. - /// - LexicalError = 5, - - /// - /// Often the parser will read a token that is not expected - /// in the grammar. When this happens, the Tokens() property - /// is filled with tokens the parsing engine expected to read. - /// To recover: push one of the expected tokens on the input queue. - /// - SyntaxError = 6, - - /// - /// The parser reached the end of the file while reading a comment. - /// This is caused when the source text contains a "run-away" - /// comment, or in other words, a block comment that lacks the - /// delimiter. - /// - CommentError = 7, - - /// - /// Something is wrong, very wrong. - /// - InternalError = 8, - - /// - /// A block comment is complete. - /// When this message is returned, the content of the CurrentComment - /// property is set to the comment text. The text includes starting and ending - /// block comment characters. - /// - CommentBlockRead = 9, - - /// - /// Line comment is read. - /// When this message is returned, the content of the CurrentComment - /// property is set to the comment text. The text includes starting - /// line comment characters. - /// - CommentLineRead = 10, - } -} diff --git a/ParserTools/ParserTools/GoldParser/Parser.cs b/ParserTools/ParserTools/GoldParser/Parser.cs deleted file mode 100644 index 61e9006ba..000000000 --- a/ParserTools/ParserTools/GoldParser/Parser.cs +++ /dev/null @@ -1,1074 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; -using System.IO; -using System.Text; -using System.Collections; -using System.Collections.Generic; - -#endregion - - -namespace GoldParser -{ - public class UnexpectedEOFinParseCommentBlock : Exception - { - } - /// - /// Pull parser which uses Grammar table to parser input stream. - /// - public sealed class Parser - { - #region Fields - - public Dictionary SepecialSymbolPrefixDirection = new Dictionary(); - //public char InsertSepecialSymbol = '\0'; - - private Grammar m_grammar; // Grammar of parsed language. - private bool m_trimReductions = true; // Allowes to minimize reduction tree. - - private TextReader m_textReader; // Data to parse. - private char[] m_buffer; // Buffer to keep current characters. - private int m_bufferSize; // Size of the buffer. - private int m_bufferStartIndex; // Absolute position of buffered first character. - private int m_charIndex; // Index of character in the buffer. - private int m_preserveChars; // Number of characters to preserve when buffer is refilled. - private int m_lineStart; // Relative position of line start to the buffer beginning. - private int m_lineLength; // Length of current source line. - private int m_lineNumber = 1; // Current line number. - private int m_commentLevel; // Keeps stack level for embedded comments - private StringBuilder m_commentText; // Current comment text. - - private SourceLineReadCallback m_sourceLineReadCallback; // Called when line reading finished. - - private Token m_token; // Current token - private Token[] m_inputTokens; // Stack of input tokens. - private int m_inputTokenCount; // How many tokens in the input. - - private LRStackItem[] m_lrStack; // Stack of LR states used for LR parsing. - private int m_lrStackIndex; // Index of current LR state in the LR parsing stack. - private LRState m_lrState; // Current LR state. - private int m_reductionCount; // Number of items in reduction. It is Undefined if no reducton available. - private Symbol[] m_expectedTokens; // What tokens are expected in case of error? - private Symbol[] m_expectedNonTerminalTokens; // What tokens are expected in case of error? - - private const int MinimumBufferSize = 4096; // Minimum size of char buffer. - private const char EndOfString = (char) 0; // Designates last string terminator. - private const int MinimumInputTokenCount = 2; // Minimum input token stack size. - private const int MinimumLRStackSize = 256; // Minimum size of reduction stack. - private const int Undefined = -1; // Used for undefined int values. - - #endregion - - #region Constructors - - /// - /// Initializes new instance of Parser class. - /// - /// TextReader instance to read data from. - /// Grammar with parsing tables to parser input stream. - public Parser(TextReader textReader, Grammar grammar) - { - if (textReader == null) - { - throw new ArgumentNullException("textReader"); - } - if (grammar == null) - { - throw new ArgumentNullException("grammar"); - } - - m_textReader = textReader; - m_bufferSize = MinimumBufferSize; - m_buffer = new char[m_bufferSize + 1]; - m_lineLength = Undefined; - ReadBuffer(); - - m_inputTokens = new Token[MinimumInputTokenCount]; - m_lrStack = new LRStackItem[MinimumLRStackSize]; - - m_grammar = grammar; - - // Put grammar start symbol into LR parsing stack. - m_lrState = m_grammar.InitialLRState; - LRStackItem start = new LRStackItem(); - start.m_token.m_symbol = m_grammar.StartSymbol; - start.m_state = m_lrState; - m_lrStack[m_lrStackIndex] = start; - - m_reductionCount = Undefined; // there are no reductions yet. - } - - #endregion - - #region Parser general properties - - /// - /// Gets the parser's grammar. - /// - public Grammar Grammar - { - get { return m_grammar; } - } - - /// - /// Gets or sets flag to trim reductions. - /// - public bool TrimReductions - { - get { return m_trimReductions; } - set { m_trimReductions = value; } - } - - #endregion - - #region Data Source properties and methods - - /// - /// Gets source of parsed data. - /// - public TextReader TextReader - { - get { return m_textReader; } - } - - /// - /// Gets current char position. - /// - public int CharPosition - { - get { return m_charIndex + m_bufferStartIndex; } - } - - /// - /// Gets current line number. It is 1-based. - /// - public int LineNumber - { - get { return m_lineNumber; } - } - - /// - /// Gets current char position in the current source line. It is 1-based. - /// - public int LinePosition - { - get { return CharPosition - m_lineStart + 1; } - } - - /// - /// Gets current source line text. It can be truncated if line is longer than 2048 characters. - /// - public string LineText - { - get - { - int lineStart = Math.Max(m_lineStart, 0); - int lineLength; - if (m_lineLength == Undefined) - { - // Line was requested outside of SourceLineReadCallback call - lineLength = m_charIndex - lineStart; - } - else - { - lineLength = m_lineLength - (lineStart - m_lineStart); - } - if (lineLength > 0) - { - return new String(m_buffer, lineStart, lineLength); - } - return string.Empty; - } - } - - /// - /// Gets or sets callback function to track source line text. - /// - public SourceLineReadCallback SourceLineReadCallback - { - get { return m_sourceLineReadCallback; } - set { m_sourceLineReadCallback = value; } - } - - /// - /// Reads next characters to the buffer. - /// - /// Number of characters read. - private int ReadBuffer() - { - // Find out how many bytes to preserve. - // We truncate long lines. - int lineStart = (m_lineStart < 0) ? 0 : m_lineStart; - int lineCharCount = m_charIndex - lineStart; - if (lineCharCount > m_bufferSize / 2) - { - lineCharCount = m_bufferSize / 2; - } - int moveIndex = m_charIndex - lineCharCount; - int moveCount = lineCharCount + m_preserveChars; - if (moveCount > 0) - { - // We need to keep current token characters. - if (m_bufferSize - moveCount < 20) - { - // Grow the buffer - m_bufferSize = m_bufferSize * 2; - char[] newBuffer = new char[m_bufferSize + 1]; - Array.Copy(m_buffer, moveIndex, newBuffer, 0, moveCount); - m_buffer = newBuffer; - } - else - { - Array.Copy(m_buffer, moveIndex, m_buffer, 0, moveCount); - } - } - - // Read as many characters as possible. - int count = m_bufferSize - moveCount; - int result = m_textReader.Read(m_buffer, moveCount, count); - // Mark character after last read one as End-Of-String - m_buffer[moveCount + result] = EndOfString; - // Adjust buffer variables. - m_bufferStartIndex += moveIndex; - m_charIndex -= moveIndex; - m_lineStart -= moveIndex; - return result; - } - - /// - /// Increments current char index by delta character positions. - /// - /// Number to increment char index. - private void MoveBy(int delta) - { - for (int i = delta; --i >= 0;) - { - if (m_buffer[m_charIndex++] == '\n') - { - if (m_sourceLineReadCallback != null) - { - m_lineLength = m_charIndex - m_lineStart - 1; // Exclude '\n' - int lastIndex = m_lineStart + m_lineLength - 1; - if (lastIndex >= 0 && m_buffer[lastIndex] == '\r') - { - m_lineLength--; - } - if (m_lineLength < 0) - { - m_lineLength = 0; - } - m_sourceLineReadCallback(this, m_lineStart + m_bufferStartIndex, m_lineLength); - } - m_lineNumber++; - m_lineStart = m_charIndex; - m_lineLength = Undefined; - } - if (m_buffer[m_charIndex] == '\0') - { - if (m_sourceLineReadCallback != null) - { - m_lineLength = m_charIndex - m_lineStart; - if (m_lineLength > 0) - { - m_sourceLineReadCallback(this, m_lineStart + m_bufferStartIndex, m_lineLength); - } - m_lineLength = Undefined; - } - } - } - } - - /// - /// Moves current char pointer to the end of source line. - /// - private void MoveToLineEnd() - { - while (true) - { - char ch = m_buffer[m_charIndex]; - switch (ch) - { - case '\r': - case '\n': - return; - - case EndOfString: - if (ReadBuffer() == 0) - { - return; - } - break; - - default: - if (m_commentText != null) - { - m_commentText.Append(ch); - } - break; - } - m_charIndex++; - } - } - - #endregion - - #region Tokenizer properties and methods - - /// - /// Gets or sets current token symbol. - /// - public Symbol TokenSymbol - { - get { return m_token.m_symbol; } - set { m_token.m_symbol = value; } - } - - /// - /// Gets or sets current token text. - /// - public string TokenText - { - get - { - if (m_token.m_text == null) - { - if (m_token.m_length > 0) - { - m_token.m_text = new String(m_buffer, m_token.m_start - m_bufferStartIndex, m_token.m_length); - } - else - { - m_token.m_text = string.Empty; - } - } - return m_token.m_text; - } - set { m_token.m_text = value; } - } - - /// - /// Gets or sets current token position relative to input stream beginning. - /// - public int TokenCharPosition - { - get { return m_token.m_start; } - set { m_token.m_start = value; } - } - - /// - /// Gets or sets current token text length. - /// - public int TokenLength - { - get { return m_token.m_length; } - set { m_token.m_length = value; } - } - - /// - /// Gets or sets current token line number. It is 1-based. - /// - public int TokenLineNumber - { - get { return m_token.m_lineNumber; } - set { m_token.m_lineNumber = value; } - } - - /// - /// Gets or sets current token position in current source line. It is 1-based. - /// - public int TokenLinePosition - { - get { return m_token.m_linePosition; } - set { m_token.m_linePosition = value; } - } - - /// - /// Gets or sets token syntax object associated with the current token or reduction. - /// - public object TokenSyntaxNode - { - get - { - if (m_reductionCount == Undefined) - { - return m_token.m_syntaxNode; - } - else - { - return m_lrStack[m_lrStackIndex].m_token.m_syntaxNode; - } - } - set - { - if (m_reductionCount == Undefined) - { - m_token.m_syntaxNode = value; - } - else - { - m_lrStack[m_lrStackIndex].m_token.m_syntaxNode = value; - } - } - } - - /// - /// Returns string representation of the token. - /// - /// String representation of the token. - public string TokenString - { - get - { - if (m_token.m_symbol.m_symbolType != SymbolType.Terminal) - { - return m_token.m_symbol.ToString(); - } - StringBuilder sb = new StringBuilder(m_token.m_length); - for (int i = 0; i < m_token.m_length; i++) - { - char ch = m_buffer[m_token.m_start - m_bufferStartIndex + i]; - if (ch < ' ') - { - switch (ch) - { - case '\n': - sb.Append("{LF}"); - break; - case '\r': - sb.Append("{CR}"); - break; - case '\t': - sb.Append("{HT}"); - break; - } - } - else - { - sb.Append(ch); - } - } - return sb.ToString(); - } - } - - /// - /// Pushes a token to the input token stack. - /// - /// Token symbol. - /// Token text. - /// Syntax node associated with the token. - public void PushInputToken(Symbol symbol, string text, object syntaxNode) - { - if (m_token.m_symbol != null) - { - if (m_inputTokenCount == m_inputTokens.Length) - { - Token[] newTokenArray = new Token[m_inputTokenCount * 2]; - Array.Copy(m_inputTokens, newTokenArray, m_inputTokenCount); - m_inputTokens = newTokenArray; - } - m_inputTokens[m_inputTokenCount++] = m_token; - } - m_token = new Token(); - m_token.m_symbol = symbol; - m_token.m_text = text; - m_token.m_length = (text != null) ? text.Length : 0; - m_token.m_syntaxNode = syntaxNode; - } - - /// - /// Pops token from the input token stack. - /// - /// Token symbol from the top of input token stack. - public Symbol PopInputToken() - { - Symbol result = m_token.m_symbol; - if (m_inputTokenCount > 0) - { - m_token = m_inputTokens[--m_inputTokenCount]; - } - else - { - m_token.m_symbol = null; - m_token.m_text = null; - } - return result; - } - - char SpecialSymbol = '\0'; - bool NotExecuteSpecialSymbol = false; - - /// - /// Reads next token from the input stream. - /// - /// Token symbol which was read. - public Symbol ReadToken() - { - m_token.m_text = null; - m_token.m_start = m_charIndex + m_bufferStartIndex; - m_token.m_lineNumber = m_lineNumber; - m_token.m_linePosition = m_charIndex /*+ m_bufferStartIndex*/ - m_lineStart + 1; - int lookahead = m_charIndex; // Next look ahead char in the input - int tokenLength = 0; - Symbol tokenSymbol = null; - DfaState[] dfaStateTable = m_grammar.m_dfaStateTable; - - char ch = m_buffer[lookahead]; - if (ch == EndOfString) - { - if (ReadBuffer() == 0) - { - m_token.m_symbol = m_grammar.m_endSymbol; - m_token.m_length = 0; - return m_token.m_symbol; - } - lookahead = m_charIndex; - ch = m_buffer[lookahead]; - } - if (SpecialSymbol != '\0') - { - ch = SpecialSymbol; - SpecialSymbol = '\0'; - lookahead--; - NotExecuteSpecialSymbol = true; - } - DfaState dfaState = m_grammar.m_dfaInitialState; - while (true) - { - dfaState = dfaState.m_transitionVector[ch] as DfaState; - - // This block-if statement checks whether an edge was found from the current state. - // If so, the state and current position advance. Otherwise it is time to exit the main loop - // and report the token found (if there was it fact one). If the LastAcceptState is -1, - // then we never found a match and the Error Token is created. Otherwise, a new token - // is created using the Symbol in the Accept State and all the characters that - // comprise it. - if (dfaState != null) - { - // This code checks whether the target state accepts a token. If so, it sets the - // appropiate variables so when the algorithm in done, it can return the proper - // token and number of characters. - lookahead++; - if (dfaState.m_acceptSymbol != null) - { - tokenSymbol = dfaState.m_acceptSymbol; - tokenLength = lookahead - m_charIndex; - } - ch = m_buffer[lookahead]; - if (ch == EndOfString) - { - m_preserveChars = lookahead - m_charIndex; - if (ReadBuffer() == 0) - { - // Found end of of stream - lookahead = m_charIndex + m_preserveChars; - } - else - { - lookahead = m_charIndex + m_preserveChars; - ch = m_buffer[lookahead]; - } - m_preserveChars = 0; - } - } - else - { - if (tokenSymbol != null) - { - m_token.m_symbol = tokenSymbol; - m_token.m_length = tokenLength; - if ((SepecialSymbolPrefixDirection.Count > 0 && SepecialSymbolPrefixDirection.ContainsKey(TokenText)) /*|| InsertSepecialSymbol!='\0'*/) - { - if (!NotExecuteSpecialSymbol) - { - NotExecuteSpecialSymbol = false; - /*if (InsertSepecialSymbol != '\0') - SpecialSymbol = InsertSepecialSymbol; - else*/ - SpecialSymbol = SepecialSymbolPrefixDirection[TokenText]; - return ReadToken(); - } - NotExecuteSpecialSymbol = false; - } - MoveBy(tokenLength); - } - else - { - //Tokenizer cannot recognize symbol - m_token.m_symbol = m_grammar.m_errorSymbol; - m_token.m_length = 1; - MoveBy(1); - } - break; - } - } - return m_token.m_symbol; - } - - /// - /// Removes current token and pops next token from the input stack. - /// - private void DiscardInputToken() - { - if (m_inputTokenCount > 0) - { - m_token = m_inputTokens[--m_inputTokenCount]; - } - else - { - m_token.m_symbol = null; - m_token.m_text = null; - } - } - - #endregion - - #region LR parser properties and methods - - /// - /// Gets current LR state. - /// - public LRState CurrentLRState - { - get { return m_lrState; } - } - - /// - /// Gets current reduction syntax rule. - /// - public Rule ReductionRule - { - get { return m_lrStack[m_lrStackIndex].m_rule; } - } - - /// - /// Gets number of items in the current reduction - /// - public int ReductionCount - { - get { return m_reductionCount; } - } - - /// - /// Gets reduction item syntax object by its index. - /// - /// Index of reduction item. - /// Syntax object attached to reduction item. - public object GetReductionSyntaxNode(int index) - { - //DarkStar Changed for Speed! - /*if (index < 0 || index >= m_reductionCount) - { - throw new IndexOutOfRangeException(); - }*/ - return m_lrStack[m_lrStackIndex - m_reductionCount + index].m_token.m_syntaxNode; - } - - /// - /// Gets array of expected token symbols. - /// - public Symbol[] GetExpectedTokens() - { - return m_expectedTokens; - } - - public Symbol[] GetExpectedNonTerminalTokens() - { - return m_expectedNonTerminalTokens; - } - - /// - /// Executes next step of parser and returns parser state. - /// - /// Parser current state. - public ParseMessage Parse() - { - if (m_token.m_symbol != null) - { - switch (m_token.m_symbol.m_symbolType) - { - case SymbolType.CommentLine: - DiscardInputToken(); //Remove it - MoveToLineEnd(); - break; - - case SymbolType.CommentStart: - ProcessBlockComment(); - break; - } - } - while (true) - { - if (m_token.m_symbol == null) - { - //We must read a token - Symbol readTokenSymbol = ReadToken(); - SymbolType symbolType = readTokenSymbol.m_symbolType; - if (m_commentLevel == 0 - && symbolType != SymbolType.CommentLine - && symbolType != SymbolType.CommentStart - && symbolType != SymbolType.WhiteSpace) - { - return ParseMessage.TokenRead; - } - } - else - { - //==== Normal parse mode - we have a token and we are not in comment mode - switch (m_token.m_symbol.m_symbolType) - { - case SymbolType.WhiteSpace: - DiscardInputToken(); // Discard Whitespace - break; - - case SymbolType.CommentStart: - m_commentLevel = 1; // Switch to block comment mode. - return ParseMessage.CommentBlockRead; - - case SymbolType.CommentLine: - return ParseMessage.CommentLineRead; - - case SymbolType.Error: - return ParseMessage.LexicalError; - - default: - //Finally, we can parse the token - TokenParseResult parseResult = ParseToken(); - switch (parseResult) - { - case TokenParseResult.Accept: - return ParseMessage.Accept; - - case TokenParseResult.InternalError: - return ParseMessage.InternalError; - - case TokenParseResult.ReduceNormal: - if (!m_lrStack[m_lrStackIndex].m_rule.m_hasEmpty) - return ParseMessage.Reduction; - break; - - case TokenParseResult.Shift: - //A simple shift, we must continue - DiscardInputToken(); // Okay, remove the top token, it is on the stack - break; - - case TokenParseResult.SyntaxError: - return ParseMessage.SyntaxError; - - default: - //Do nothing - break; - } - break; - } - } - } - } - - private void ProcessBlockComment() - { - if (m_commentLevel > 0) - { - if (m_commentText != null) - { - m_commentText.Append(TokenText); - } - DiscardInputToken(); - while (true) - { - SymbolType symbolType = ReadToken().SymbolType; - if (m_commentText != null) - { - m_commentText.Append(TokenText); - } - DiscardInputToken(); - switch (symbolType) - { - case SymbolType.CommentStart: - //Commented by MZ for using simple comment blocks - //m_commentLevel++; - break; - - case SymbolType.CommentEnd: - m_commentLevel--; - if (m_commentLevel == 0) - { - // Done with comment. - return; - } - break; - - case SymbolType.End: - //TODO: replace with special exception. - throw new UnexpectedEOFinParseCommentBlock(); - - default: - //Do nothing, ignore - //The 'comment line' symbol is ignored as well - break; - } - } - } - } - - /// - /// Gets current comment text. - /// - public string CommentText - { - get - { - if (m_token.m_symbol != null) - { - switch (m_token.m_symbol.m_symbolType) - { - case SymbolType.CommentLine: - m_commentText = new StringBuilder(); - m_commentText.Append(TokenText); - DiscardInputToken(); //Remove token - MoveToLineEnd(); - string lineComment = m_commentText.ToString(); - m_commentText = null; - return lineComment; - - case SymbolType.CommentStart: - m_commentText = new StringBuilder(); - ProcessBlockComment(); - string blockComment = m_commentText.ToString(); - m_commentText = null; - return blockComment; - } - } - return String.Empty; - } - } - - private void RsesizeLRStackIfNeed(int index) - { - if (index > m_lrStack.Length - 1) - { - LRStackItem[] tmp = new LRStackItem[m_lrStack.Length * 2]; - m_lrStack.CopyTo(tmp, 0); - m_lrStack = tmp; - } - } - - private TokenParseResult ParseToken() - { - LRStateAction stateAction = m_lrState.m_transitionVector[m_token.m_symbol.m_index]; - if (stateAction != null) - { - //Work - shift or reduce - if (m_reductionCount > 0) - { - int newIndex = m_lrStackIndex - m_reductionCount; - m_lrStack[newIndex] = m_lrStack[m_lrStackIndex]; - m_lrStackIndex = newIndex; - } - m_reductionCount = Undefined; - switch (stateAction.Action) - { - case LRAction.Accept: - m_reductionCount = 0; - return TokenParseResult.Accept; - - case LRAction.Shift: - m_lrState = m_grammar.m_lrStateTable[stateAction.m_value]; - LRStackItem nextToken = new LRStackItem(); - nextToken.m_token = m_token; - nextToken.m_state = m_lrState; - RsesizeLRStackIfNeed(++m_lrStackIndex); - m_lrStack[m_lrStackIndex] = nextToken; - return TokenParseResult.Shift; - - case LRAction.Reduce: - //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token - Rule currentRule = m_grammar.m_ruleTable[stateAction.m_value]; - LRStackItem head; - LRState nextState; - if (m_trimReductions && currentRule.m_hasOneNonTerminal) - { - head = m_lrStack[m_lrStackIndex]; - head.m_token.m_symbol = currentRule.m_nonTerminal; - head.m_token.m_text = null; - nextState = m_lrStack[m_lrStackIndex - 1].m_state; - head.m_state = m_lrState = m_grammar.m_lrStateTable[nextState.m_transitionVector[currentRule.m_nonTerminal.m_index].m_value]; - m_lrStack[m_lrStackIndex] = head; - return TokenParseResult.ReduceEliminated; - } - else - { - head = new LRStackItem(); - head.m_rule = currentRule; - head.m_token.m_symbol = currentRule.m_nonTerminal; - head.m_token.m_text = null; - m_reductionCount = currentRule.m_symbols.Length; - nextState = m_lrStack[m_lrStackIndex - m_reductionCount].m_state; - m_lrState = m_grammar.m_lrStateTable[nextState.m_transitionVector[currentRule.m_nonTerminal.m_index].m_value]; - head.m_state = m_lrState; - RsesizeLRStackIfNeed(++m_lrStackIndex); - m_lrStack[m_lrStackIndex] = head; - return TokenParseResult.ReduceNormal; - } - - /* - //======== Create Reduction - LRStackItem head; - TokenParseResult parseResult; - LRState nextState; - if (m_trimReductions && currentRule.m_hasOneNonTerminal) - { - //The current rule only consists of a single nonterminal and can be trimmed from the - //parse tree. Usually we create a new Reduction, assign it to the Data property - //of Head and push it on the stack. However, in this case, the Data property of the - //Head will be assigned the Data property of the reduced token (i.e. the only one - //on the stack). - //In this case, to save code, the value popped of the stack is changed into the head. - head = m_lrStack[m_lrStackIndex]; - head.m_token.m_symbol = currentRule.m_nonTerminal; - head.m_token.m_text = null; - parseResult = TokenParseResult.ReduceEliminated; - //========== Goto - nextState = m_lrStack[m_lrStackIndex - 1].m_state; - } - else - { - //Build a Reduction - head = new LRStackItem(); - head.m_rule = currentRule; - head.m_token.m_symbol = currentRule.m_nonTerminal; - head.m_token.m_text = null; - m_reductionCount = currentRule.m_symbols.Length; - parseResult = TokenParseResult.ReduceNormal; - //========== Goto - nextState = m_lrStack[m_lrStackIndex - m_reductionCount].m_state; - } - - //========= If nextAction is null here, then we have an Internal Table Error!!!! - LRStateAction nextAction = nextState.m_transitionVector[currentRule.m_nonTerminal.m_index]; - if (nextAction != null) - { - m_lrState = m_grammar.m_lrStateTable[nextAction.m_value]; - head.m_state = m_lrState; - if (parseResult == TokenParseResult.ReduceNormal) - { - m_lrStack[++m_lrStackIndex] = head; - } - else - { - m_lrStack[m_lrStackIndex] = head; - } - return parseResult; - } - else - { - return TokenParseResult.InternalError; - } - */ - } - } - - //=== Syntax Error! Fill Expected Tokens - m_expectedTokens = new Symbol[m_lrState.ActionCount]; - m_expectedNonTerminalTokens = new Symbol[m_lrState.ActionCount]; - int lengtht = 0,lengthnt=0; - for (int i = 0; i < m_lrState.ActionCount; i++) - { - switch (m_lrState.GetAction(i).Symbol.SymbolType) - { - case SymbolType.NonTerminal: - m_expectedNonTerminalTokens[lengthnt++] = m_lrState.GetAction(i).Symbol; - m_expectedTokens[lengtht++] = m_lrState.GetAction(i).Symbol; - break; - case SymbolType.Terminal: - case SymbolType.End: - m_expectedTokens[lengtht++] = m_lrState.GetAction(i).Symbol; - break; - } - } - if (lengtht < m_expectedTokens.Length) - { - Symbol[] newArray = new Symbol[lengtht]; - Array.Copy(m_expectedTokens, newArray, lengtht); - m_expectedTokens = newArray; - } - if (lengthnt < m_expectedNonTerminalTokens.Length) - { - Symbol[] newArray = new Symbol[lengthnt]; - Array.Copy(m_expectedNonTerminalTokens, newArray, lengthnt); - m_expectedNonTerminalTokens = newArray; - } - return TokenParseResult.SyntaxError; - } - - #endregion - - #region TokenParseResult enumeration - - /// - /// Result of parsing token. - /// - private enum TokenParseResult - { - Empty = 0, - Accept = 1, - Shift = 2, - ReduceNormal = 3, - ReduceEliminated = 4, - SyntaxError = 5, - InternalError = 6 - } - - #endregion - - #region Token struct - - /// - /// Represents data about current token. - /// - private struct Token - { - internal Symbol m_symbol; // Token symbol. - internal string m_text; // Token text. - internal int m_start; // Token start stream start. - internal int m_length; // Token length. - internal int m_lineNumber; // Token source line number. (1-based). - internal int m_linePosition; // Token position in source line (1-based). - internal object m_syntaxNode; // Syntax node which can be attached to the token. - } - - #endregion - - #region LRStackItem struct - - /// - /// Represents item in the LR parsing stack. - /// - private struct LRStackItem - { - internal Token m_token; // Token in the LR stack item. - internal LRState m_state; // LR state associated with the item. - internal Rule m_rule; // Reference to a grammar rule if the item contains non-terminal. - } - - #endregion - } -} diff --git a/ParserTools/ParserTools/GoldParser/Rule.cs b/ParserTools/ParserTools/GoldParser/Rule.cs deleted file mode 100644 index c69fa5db4..000000000 --- a/ParserTools/ParserTools/GoldParser/Rule.cs +++ /dev/null @@ -1,135 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; -using System.Text; - -#endregion - -namespace GoldParser -{ - /// - /// Rule is the logical structures of the grammar. - /// - /// - /// Rules consist of a head containing a nonterminal - /// followed by a series of both nonterminals and terminals. - /// - public class Rule - { - private int m_index; - internal Symbol m_nonTerminal; - internal Symbol[] m_symbols; - internal bool m_hasOneNonTerminal; - internal bool m_hasEmpty; - - /// - /// Creates a new instance of Rule class. - /// - /// Index of the rule in the grammar rule table. - /// Nonterminal of the rule. - /// Terminal and nonterminal symbols of the rule. - public Rule(int index, Symbol nonTerminal, Symbol[] symbols) - { - m_index = index; - m_nonTerminal = nonTerminal; - m_symbols = symbols; - m_hasOneNonTerminal = (symbols.Length == 1) - && (symbols[0].SymbolType == SymbolType.NonTerminal); - m_hasEmpty=symbols.Length == 0;// && m_nonTerminal.Name == "empty"; - - } - - /// - /// Gets index of the rule in the rule table. - /// - public int Index - { - get { return m_index; } - } - - /// - /// Gets the head symbol of the rule. - /// - public Symbol NonTerminal - { - get { return m_nonTerminal; } - } - - /// - /// Gets name of the rule. - /// - public string Name - { - get { return '<' + m_nonTerminal.Name + '>'; } - } - - /// - /// Gets number of symbols. - /// - public int Count - { - get { return m_symbols.Length; } - } - - /// - /// Gets symbol by its index. - /// - public Symbol this[int index] - { - get { return m_symbols[index]; } - } - - /// - /// Gets true if the rule contains exactly one symbol. - /// - /// Used by the Parser object to TrimReductions - public bool ContainsOneNonTerminal - { - get { return m_hasOneNonTerminal; } - } - - /// - /// Gets the rule definition. - /// - public string Definition - { - get - { - StringBuilder result = new StringBuilder(); - for (int i = 0; i < m_symbols.Length; i++) - { - result.Append(m_symbols[i].ToString()); - if (i < m_symbols.Length - 1) - result.Append(' '); - } - return result.ToString(); - } - } - - /// - /// Returns the Backus-Noir representation of the rule. - /// - /// - public override string ToString() - { - return Name + " ::= " + Definition; - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/SR.cs b/ParserTools/ParserTools/GoldParser/SR.cs deleted file mode 100644 index 8b81cc65a..000000000 --- a/ParserTools/ParserTools/GoldParser/SR.cs +++ /dev/null @@ -1,100 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; -using System.IO; -using System.Resources; -using System.Globalization; - -#endregion - -namespace GoldParser -{ - /// - /// Custom resource class. Usage: - /// string s = Res.GetString(Res.MyIdenfitier); - /// - internal sealed class SR - { - static SR ms_loader = new SR(); - - private ResourceManager m_resources; - - private SR() - { - m_resources = new ResourceManager("GoldParser", this.GetType().Module.Assembly); - } - - /* These function can be useful in other applications. - - public static string GetString(string name, params object[] args) - { - // null CultureInfo: let ResouceManager determine the culture - return GetString(null, name, args); - } - - public static string GetString(CultureInfo culture, string name, params object[] args) - { - string res = ms_loader.m_resources.GetString(name, culture); - - if (args != null && args.Length > 0) - { - return String.Format(culture, res, args); - } - else - { - return res; - } - } -*/ - public static string GetString(string name) - { - return GetString(null, name); - } - - public static string GetString(CultureInfo culture, string name) - { - return ms_loader.m_resources.GetString(name, culture); - } - - // Code below is automatically generated by GenResNm.exe. - // Do not modify it manually. - #region Resource String Names - - internal const string Grammar_WrongFileHeader = "Grammar_WrongFileHeader"; - - internal const string Grammar_InvalidRecordType = "Grammar_InvalidRecordType"; - - internal const string Grammar_NoEntry = "Grammar_NoEntry"; - - internal const string Grammar_EmptyEntryExpected = "Grammar_EmptyEntryExpected"; - - internal const string Grammar_StringEntryExpected = "Grammar_StringEntryExpected"; - - internal const string Grammar_IntegerEntryExpected = "Grammar_IntegerEntryExpected"; - - internal const string Grammar_ByteEntryExpected = "Grammar_ByteEntryExpected"; - - internal const string Grammar_BooleanEntryExpected = "Grammar_BooleanEntryExpected"; - - internal const string Grammar_InvalidRecordHeader = "Grammar_InvalidRecordHeader"; - - #endregion - } -} diff --git a/ParserTools/ParserTools/GoldParser/SourceLineReadCallback.cs b/ParserTools/ParserTools/GoldParser/SourceLineReadCallback.cs deleted file mode 100644 index efd75abe8..000000000 --- a/ParserTools/ParserTools/GoldParser/SourceLineReadCallback.cs +++ /dev/null @@ -1,31 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// This callback is used by parser to notify read source line. - /// Use parser.LineText to get line source. - /// - public delegate void SourceLineReadCallback(Parser parser, int lineStart, int lineLength); -} diff --git a/ParserTools/ParserTools/GoldParser/Symbol.cs b/ParserTools/ParserTools/GoldParser/Symbol.cs deleted file mode 100644 index 5e737456c..000000000 --- a/ParserTools/ParserTools/GoldParser/Symbol.cs +++ /dev/null @@ -1,142 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; -using System.Text; - -#endregion - -namespace GoldParser -{ - /// - /// Represents a terminal or nonterminal symbol used by the Deterministic - /// Finite Automata (DFA) and LR Parser. - /// - /// - /// Symbols can be either terminals (which represent a class of - /// tokens - such as identifiers) or nonterminals (which represent - /// the rules and structures of the grammar). Terminal symbols fall - /// into several categories for use by the GOLD Parser Engine - /// which are enumerated in SymbolType enumeration. - /// - public class Symbol - { - internal int m_index; // symbol index in symbol table - private string m_name; // name of the symbol - internal SymbolType m_symbolType; // type of the symbol - private string m_text; // printable representation of symbol - - private const string m_quotedChars = "|-+*?()[]{}<>!"; - - /// - /// Creates a new instance of Symbol class. - /// - /// Symbol index in symbol table. - /// Name of the symbol. - /// Type of the symbol. - public Symbol(int index, string name, SymbolType symbolType) - { - m_index = index; - m_name = name; - m_symbolType = symbolType; - } - - /// - /// Returns the index of the symbol in the GOLDParser object's Symbol Table. - /// - public int Index - { - get { return m_index; } - } - - /// - /// Returns the name of the symbol. - /// - public string Name - { - get { return m_name; } - } - - /// - /// Returns an enumerated data type that denotes - /// the class of symbols that the object belongs to. - /// - public SymbolType SymbolType - { - get { return m_symbolType; } - } - - /// - /// Returns the text representation of the symbol. - /// In the case of nonterminals, the name is delimited by angle brackets, - /// special terminals are delimited by parenthesis - /// and terminals are delimited by single quotes - /// (if special characters are present). - /// - /// String representation of symbol. - public override string ToString() - { - if (m_text == null) - { - switch (SymbolType) - { - case SymbolType.NonTerminal: - m_text = '<' + Name + '>'; - break; - - case SymbolType.Terminal: - m_text = FormatTerminalSymbol(Name); - break; - - default: - m_text = '(' + Name + ')'; - break; - } - } - return m_text; - } - - private static string FormatTerminalSymbol(string source) - { - StringBuilder result = new StringBuilder(); - for (int i = 0; i < source.Length; i++) - { - char ch = source[i]; - if (ch == '\'') - { - result.Append("''"); - } - else if (IsQuotedChar(ch) || (ch == '"')) - { - result.Append(new Char[] {'\'', ch, '\''}); - } - else - { - result.Append(ch); - } - } - return result.ToString(); - } - - private static bool IsQuotedChar(char value) - { - return (m_quotedChars.IndexOf(value) >= 0); - } - } -} diff --git a/ParserTools/ParserTools/GoldParser/SymbolType.cs b/ParserTools/ParserTools/GoldParser/SymbolType.cs deleted file mode 100644 index 2b67069e0..000000000 --- a/ParserTools/ParserTools/GoldParser/SymbolType.cs +++ /dev/null @@ -1,81 +0,0 @@ -#region Copyright - -//---------------------------------------------------------------------- -// Gold Parser engine. -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Vladimir Morozov (vmoroz@hotmail.com) -// -// The translation is based on the other engine translations: -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -#endregion - -#region Using directives - -using System; - -#endregion - -namespace GoldParser -{ - /// - /// Type of symbol. - /// - public enum SymbolType - { - /// - /// Normal nonterminal - /// - NonTerminal = 0, - - /// - /// Normal terminal - /// - Terminal = 1, - - /// - /// This Whitespace symbols is a special terminal - /// that is automatically ignored the the parsing engine. - /// Any text accepted as whitespace is considered - /// to be inconsequential and "meaningless". - /// - WhiteSpace = 2, - - /// - /// The End symbol is generated when the tokenizer - /// reaches the end of the source text. - /// - End = 3, - - /// - /// This type of symbol designates the start of a block quote. - /// - CommentStart = 4, - - /// - /// This type of symbol designates the end of a block quote. - /// - CommentEnd = 5, - - /// - /// When the engine reads a token that is recognized as - /// a line comment, the remaining characters on the line - /// are automatically ignored by the parser. - /// - CommentLine = 6, - - /// - /// The Error symbol is a general-purpose means - /// of representing characters that were not recognized - /// by the tokenizer. In other words, when the tokenizer - /// reads a series of characters that is not accepted - /// by the DFA engine, a token of this type is created. - /// - Error = 7 - } -} diff --git a/Parsers/PascalABCParserNewSaushkin/Errors1.cs b/Parsers/PascalABCParserNewSaushkin/Errors1.cs index 0954e4c11..61a399774 100644 --- a/Parsers/PascalABCParserNewSaushkin/Errors1.cs +++ b/Parsers/PascalABCParserNewSaushkin/Errors1.cs @@ -67,22 +67,6 @@ namespace PascalABCCompiler.Errors public class PABCNETUnexpectedToken : SyntaxError { private string _message; - public PABCNETUnexpectedToken(PascalABCCompiler.ParserTools.GPBParser parser) - : base("", parser.current_file_name, parser.parsertools.GetTokenSourceContext(parser.LRParser), (syntax_tree_node)parser.prev_node) - { - List Symbols = parser.parsertools.GetPrioritySymbols(parser.LRParser.GetExpectedTokens()); - if (Symbols.Count == 1) - { - string OneSybmolText = "ONE_" + Symbols[0].Name.ToUpper(); - string LocOneSybmolText = PascalABCSavParser.StringResources.Get(OneSybmolText); - if (LocOneSybmolText != OneSybmolText) - { - _message = string.Format(LocOneSybmolText); - return; - } - } - _message = string.Format(PascalABCSavParser.StringResources.Get("EXPECTED{0}"), PascalABCCompiler.FormatTools.ObjectsToString(parser.parsertools.SymbolsToStrings(Symbols.ToArray()), ",")); - } public PABCNETUnexpectedToken(string _file_name, string expected, SourceContext _source_context, syntax_tree_node _node) : base("", _file_name, _source_context, _node) { diff --git a/README.md b/README.md index 6495518cc..f04c11098 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Build the project and run tests. ```bash $ git clone https://github.com/pascalabcnet/pascalabcnet $ cd pascalabcnet -$ sudo sh _RebuildReleaseAndRunTests.sh +$ sh _RebuildReleaseAndRunTests.sh ``` ## Building PascalABC.NET in MacOS @@ -32,7 +32,7 @@ Download and install Mono from official page. Install the git-client. Run the co ```bash $ git clone https://github.com/pascalabcnet/pascalabcnet $ cd pascalabcnet -$ sudo sh _RebuildReleaseAndRunTests.sh +$ sh _RebuildReleaseAndRunTests.sh ``` ### Run the PascalABC.NET command line compiler @@ -73,7 +73,7 @@ sudo apt-get install mono-complete ```bash $ git clone https://github.com/pascalabcnet/pascalabcnet $ cd pascalabcnet -$ sudo sh _RebuildReleaseAndRunTests.sh +$ sh _RebuildReleaseAndRunTests.sh ``` ## Сборка проекта в MacOS @@ -81,7 +81,7 @@ $ sudo sh _RebuildReleaseAndRunTests.sh ```bash $ git clone https://github.com/pascalabcnet/pascalabcnet $ cd pascalabcnet -$ sudo sh _RebuildReleaseAndRunTests.sh +$ sh _RebuildReleaseAndRunTests.sh ``` ### Запуск diff --git a/ReleaseGenerators/PABCRtl/PABCRtl.pas b/ReleaseGenerators/PABCRtl/PABCRtl.pas index 3419d55c1..0cff9fb12 100644 --- a/ReleaseGenerators/PABCRtl/PABCRtl.pas +++ b/ReleaseGenerators/PABCRtl/PABCRtl.pas @@ -2,8 +2,8 @@ library PABCRtl; uses PABCSystem, ABCButtons, ABCHouse, ABCObjects, - ABCSprites, BFSystem, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, - DrawManField, Events, FilesOperations, GOLDParserEngine, GraphABC, + ABCSprites, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, + DrawManField, Events, FilesOperations, GraphABC, GraphABCHelper, IniFile, PointerTools, PointRect, Robot, RobotField, RobotTaskMaker, RobotZadan, Sockets, Utils, VCL, Timers, PT4TaskMakerNET, RBDMUtils, FormsABC, Collections, Arrays, ClientServer, __RedirectIOMode, __RunMode, __RtlUtils; diff --git a/ReleaseGenerators/PABCRtl/PABCRtl32.pas b/ReleaseGenerators/PABCRtl/PABCRtl32.pas index e35d819fd..40c798690 100644 --- a/ReleaseGenerators/PABCRtl/PABCRtl32.pas +++ b/ReleaseGenerators/PABCRtl/PABCRtl32.pas @@ -2,8 +2,8 @@ library PABCRtl32; uses PABCSystem, ABCButtons, ABCHouse, ABCObjects, - ABCSprites, BFSystem, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, - DrawManField, Events, FilesOperations, GOLDParserEngine, GraphABC, + ABCSprites, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, + DrawManField, Events, FilesOperations, GraphABC, GraphABCHelper, IniFile, PointerTools, PointRect, Robot, RobotField, RobotTaskMaker, RobotZadan, Sockets, Utils, VCL, Timers, PT4, PT4TaskMakerNET, RBDMUtils, FormsABC, Collections, Arrays, ClientServer, __RedirectIOMode, __RunMode, __RtlUtils; diff --git a/ReleaseGenerators/PT4/Lib/PT4ExamBegin_ru.dll b/ReleaseGenerators/PT4/Lib/PT4ExamBegin_ru.dll index b3e5ef917..8015453af 100644 Binary files a/ReleaseGenerators/PT4/Lib/PT4ExamBegin_ru.dll and b/ReleaseGenerators/PT4/Lib/PT4ExamBegin_ru.dll differ diff --git a/ReleaseGenerators/PT4/Lib/PT4ExamCheck_ru.dll b/ReleaseGenerators/PT4/Lib/PT4ExamCheck_ru.dll index ba42e2c15..25634eb42 100644 Binary files a/ReleaseGenerators/PT4/Lib/PT4ExamCheck_ru.dll and b/ReleaseGenerators/PT4/Lib/PT4ExamCheck_ru.dll differ diff --git a/ReleaseGenerators/PT4/Lib/PT4ExamTaskC_ru.dll b/ReleaseGenerators/PT4/Lib/PT4ExamTaskC_ru.dll index 43c826627..825a50314 100644 Binary files a/ReleaseGenerators/PT4/Lib/PT4ExamTaskC_ru.dll and b/ReleaseGenerators/PT4/Lib/PT4ExamTaskC_ru.dll differ diff --git a/ReleaseGenerators/RebuildStandartModules.pas b/ReleaseGenerators/RebuildStandartModules.pas index 646c1b426..ecd9811a1 100644 --- a/ReleaseGenerators/RebuildStandartModules.pas +++ b/ReleaseGenerators/RebuildStandartModules.pas @@ -1,7 +1,7 @@ uses PABCSystem, __RedirectIOMode, __RunMode, ABCButtons, ABCHouse, ABCObjects, - ABCSprites, BFSystem, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, - DrawManField, Events, FilesOperations, GOLDParserEngine, GraphABC, + ABCSprites, Colors, CRT, DMCollect, DMTaskMaker, DMZadan, Drawman, + DrawManField, Events, FilesOperations, GraphABC, GraphABCHelper, IniFile, PointerTools, PointRect, PT4, Robot, RobotField, RobotTaskMaker, RobotZadan, Sockets, Utils, VCL, Timers, PT4Exam, PT4TaskMakerNET, RBDMUtils, Collections, Arrays, Core, FormsABC, MPI, ClientServer; diff --git a/ReleaseGenerators/sect_Core.nsh b/ReleaseGenerators/sect_Core.nsh index 2a21e6137..681ae13f0 100644 --- a/ReleaseGenerators/sect_Core.nsh +++ b/ReleaseGenerators/sect_Core.nsh @@ -63,7 +63,6 @@ File ..\bin\Lib\ABCObjects.pcu File ..\bin\Lib\ABCSprites.pcu File ..\bin\Lib\Arrays.pcu - File ..\bin\Lib\BFSystem.pcu File ..\bin\Lib\Colors.pcu File ..\bin\Lib\CRT.pcu File ..\bin\Lib\DMCollect.pcu @@ -74,7 +73,6 @@ File ..\bin\Lib\Events.pcu File ..\bin\Lib\FilesOperations.pcu File ..\bin\Lib\FormsABC.pcu - File ..\bin\Lib\GOLDParserEngine.pcu File ..\bin\Lib\GraphABC.pcu File ..\bin\Lib\GraphABCHelper.pcu File ..\bin\Lib\IniFile.pcu diff --git a/TreeConverter/TreeConversion/syntax_tree_visitor.cs b/TreeConverter/TreeConversion/syntax_tree_visitor.cs index 01468bb4a..f146c80ca 100644 --- a/TreeConverter/TreeConversion/syntax_tree_visitor.cs +++ b/TreeConverter/TreeConversion/syntax_tree_visitor.cs @@ -8515,13 +8515,23 @@ namespace PascalABCCompiler.TreeConverter si_right,get_location(id_right)); return create_static_method_call(fn,get_location(id_right),tn,false); */ - //if (!(si_right.sym_info is common_in_function_function_node)) + //if (!(si_right.sym_info is common_in_function_function_node)) + if (dn is compiled_function_node) + { + if ((dn as compiled_function_node).ConnectedToType == tn) + AddError(new UndefinedNameReference(id_right.name, get_location(id_right))); + } + else if (dn is common_namespace_function_node) + { + if ((dn as common_namespace_function_node).ConnectedToType == tn) + AddError(new UndefinedNameReference(id_right.name, get_location(id_right))); + } if (dn.semantic_node_type == semantic_node_type.indefinite_definition_node) { return new indefinite_reference(dn as indefinite_definition_node, get_location(id_right)); } - return make_delegate_wrapper(null, si_right, get_location(id_right), true); - + return make_delegate_wrapper(null, si_right, get_location(id_right), true); + } case general_node_type.property_node: { @@ -12165,6 +12175,8 @@ namespace PascalABCCompiler.TreeConverter } case proc_attribute.attr_extension: { + if (context.converted_compiled_type != null) + AddError(get_location(_procedure_attributes_list), "EXTENSIONMETHOD_KEYWORD_NOT_ALLOWED"); if (in_interface_part) AddError(get_location(_procedure_attributes_list), "EXTENSION_METHODS_IN_INTERFACE_PART_NOT_ALLOWED"); if (!(context.top_function is common_namespace_function_node)) diff --git a/_RebuildReleaseAndRunTests.sh b/_RebuildReleaseAndRunTests.sh index 9707cede7..106790ecb 100644 --- a/_RebuildReleaseAndRunTests.sh +++ b/_RebuildReleaseAndRunTests.sh @@ -29,8 +29,8 @@ if [ $? -eq 0 ]; then sn -R PABCRtl32.dll KeyPair32.snk sn -Vu PABCRtl32.dll cp PABCRtl32.dll ../../bin/Lib - gacutil -u PABCRtl - gacutil -i ../../bin/Lib/PABCRtl.dll + sudo gacutil -u PABCRtl + sudo gacutil -i ../../bin/Lib/PABCRtl.dll cd .. mono ../bin/pabcnetc.exe RebuildStandartModules.pas /rebuild if [ $? -eq 0 ]; then diff --git a/bin/Lib/BFSystem.pas b/bin/Lib/BFSystem.pas deleted file mode 100644 index 14bb54da8..000000000 --- a/bin/Lib/BFSystem.pas +++ /dev/null @@ -1,79 +0,0 @@ -unit BFSystem; - -//BF language support -//Version 2.1 -//Copyright (c) 2006-2007 DarkStar - -{$apptype console} - -interface - -uses System, PABCSystem, CRT; - -const - BufferSize = 30000; - ConsoleTextColor = Green; - InFileExt = '.in'; - OutFileExt = '.out'; - -var - BFField := new byte[BufferSize]; - BFCaretPos:integer; - BFOutFile:Text; - BFInFile:Text; - -procedure WriteCaretValue; //. -procedure ReadCaretValue; //, - -implementation - -var ReadFromInFile:boolean; - -procedure BFInit; -var fin_name: string; -begin - BFCaretPos := 0; - fin_name := ChangeFileNameExtension(GetEXEFileName, InFileExt); - ReadFromInFile := FileExists(fin_name); - if ReadFromInFile then begin - AssignFile(BFInFile, fin_name); - Reset(BFInFile); - end; - AssignFile(BFOutFile,ChangeFileNameExtension(GetEXEFileName, OutFileExt)); - Rewrite(BFOutFile); - TextColor(ConsoleTextColor); -end; - -procedure BFClose; -begin - CloseFile(BFInFile); - CloseFile(BFOutFile); -end; - -procedure WriteCaretValue; -var c: char; -begin - c := char(BFField[BFCaretPos]); - Write(c); - Write(BFOutFile, c); -end; - -procedure ReadCaretValue; -var c: char; -begin - if ReadFromInFile then begin - if EOF(BFInFile) then - BFField[BFCaretPos] := 0 - else begin - Read(BFInFile, c); - BFField[BFCaretPos] := byte(c); - end; - end else - BFField[BFCaretPos] := byte(CurrentIOSystem.read_symbol); -end; - -initialization - BFInit; -finalization - BFClose; -end. diff --git a/bin/Lib/GOLDParserEngine.pas b/bin/Lib/GOLDParserEngine.pas deleted file mode 100644 index 809ae518b..000000000 --- a/bin/Lib/GOLDParserEngine.pas +++ /dev/null @@ -1,1717 +0,0 @@ -unit GOLDParserEngine; - -{$NullBasedStrings true} - -//---------------------------------------------------------------------- -// Gold Parser engine for PascalABC.NET, v0.1 -// Project site http://pascalabc.net/ -// See more details on http://www.devincook.com/goldparser/ -// -// Original code is written in VB by Devin Cook (GOLDParser@DevinCook.com) -// -// This translation is done by Tkachuk Aleksander (darkstarta@mail.ru) -// -// The translation is based on the other engine translations: -// C# engine by Vladimir Morozov (vmoroz@hotmail.com) -// Delphi engine by Alexandre Rai (riccio@gmx.at) -// C# engine by Marcus Klimstra (klimstra@home.nl) -//---------------------------------------------------------------------- - -interface - -uses System, - System.Text, - System.IO; - -const - //SymbolType enum - SymbolTypeNonTerminal = 0; - SymbolTypeTerminal = 1; - SymbolTypeWhiteSpace = 2; - SymbolTypeEnd = 3; - SymbolTypeCommentStart= 4; - SymbolTypeCommentEnd = 5; - SymbolTypeCommentLine = 6; - SymbolTypeError = 7; - //LRAction enum - LRActionNone = 0; - LRActionShift = 1; - LRActionReduce = 2; - LRActionGoto = 3; - LRActionAccept = 4; - LRActionError = 5; - //const in ObjectMap - MAXINDEX = 255; - GROWTH = 32; - MINSIZE = 32; - MAXARRAYCOUNT = 12; - INVALIDKEY = MaxInt; - //ParseMessage enum - ParseMessageEmpty = 0; - ParseMessageTokenRead = 1; - ParseMessageReduction = 2; - ParseMessageAccept = 3; - ParseMessageNotLoadedError = 4; - ParseMessageLexicalError = 5; - ParseMessageSyntaxError = 6; - ParseMessageCommentError = 7; - ParseMessageInternalError = 8; - ParseMessageCommentBlockRead = 9; - ParseMessageCommentLineRead = 10; - //enum RecordType - RecordTypeParameters = 80; //'P' - RecordTypeTableCounts = 84; //'T' - RecordTypeInitial = 73; //'I' - RecordTypeSymbols = 83; //'S' - RecordTypeCharSets = 67; //'C' - RecordTypeRules = 82; //'R' - RecordTypeDfaStates = 68; //'D' - RecordTypeLRStates = 76; //'L' - RecordTypeComment = 33; //'!' - //enum EntryType - EntryTypeEmpty = 69; //'E' - EntryTypeInteger = 73; //'I' - EntryTypeString = 83; //'S' - EntryTypeBoolean = 66; //'B' - EntryTypeByte = 98; //'b' - //enum TokenParseResult - TokenParseResultEmpty = 0; - TokenParseResultAccept = 1; - TokenParseResultShift = 2; - TokenParseResultReduceNormal = 3; - TokenParseResultReduceEliminated = 4; - TokenParseResultSyntaxError = 5; - TokenParseResultInternalError = 6; - - charEnter = #13; - charReset = #10; - charTab = #9; - charEndOfFile = #0; - - - -type - ParseMessage = integer; - TokenParseResult = integer; - - Symbol = class - Index: integer; // symbol index in symbol table - Name: string; // name of the symbol - SymbolType: integer; // type of the symbol - m_text: string; // printable representation of symbol - m_quotedChars: string; - public - constructor(index:integer; name:string; symType:integer); - function IsQuotedChar(value:char): boolean; - function FormatTerminalSymbol(source:string):string; - function ToString:string; override; - end; - - Entry = record //internal in ObjectMap - Key: integer; - Value: object; - end; - - MapProvider = class - m_count:integer; - constructor;begin end; - function GetIndex(key:integer):object;virtual; - property Index[i:integer]:object read GetIndex; default; - function GetEntry(key:integer):Entry;virtual; - procedure Add(key:integer;value:object);virtual; - procedure Remove(key:integer);virtual; - end; - - SortedMapProvider = class(MapProvider) - m_entries: array of Entry; - m_lastKey:integer; - constructor(capacity:integer); - function GetIndex(key:integer):object; override;//possible error! - function GetEntry(key:integer):Entry;override; - procedure Add(key:integer;value:object);override; - procedure Remove(key:integer);virtual; - function FindIndex(key:integer):integer; //possible error! - function FindInsertIndex(key:integer;var found:boolean):integer;//possible error! - end; - - IndexMapProvider = class(MapProvider) - m_array: array of object; - constructor; - function GetIndex(key:integer):object; override; - function GetEntry(key:integer):Entry;override; - procedure Add(key:integer;value:object);override; - end; - - ArrayMapProvider = class(MapProvider) - m_entries: array of Entry; - constructor(capacity:integer); - function GetIndex(key:integer):object; override; - function GetEntry(key:integer):Entry;override; - procedure Add(key:integer;value:object);override; - end; - - Unassigned = class - constructor;begin end; - end; - - ObjectMap = class - m_readonly: boolean; - m_mapProvider: MapProvider; - constructor; - function getCount:integer; - property Count:integer read GetCount; - procedure SetReadOnly(value: boolean); - function GetIndex(key:integer):object; - procedure SetIndex(key:integer; value:object); - property ReadOnly:boolean read m_readonly write SetReadOnly; - public - property Index[i:integer]:object read GetIndex write SetIndex; default; - function GetKey(index:integer):integer; - procedure Remove(key:integer); - procedure Add(key:integer;value:object); - procedure SetMapProvider(readOnly:boolean); - end; - - DfaState = class - Index: integer; - AcceptSymbol: Symbol; - m_transitionVector: ObjectMap; - constructor(Index:integer;acceptSymbol:Symbol;transitionVector:ObjectMap); - end; - - LRStateAction = class - Index: integer; - StateSymbol: Symbol; - Action: integer; - Value: integer; - constructor(Index:integer;StateSymbol:Symbol;Action:integer;Value:integer); - end; - - LRState = class - Index: integer; - m_actions: array of LRStateAction; - m_transitionVector: array of LRStateAction; - constructor(Index:integer; actions: array of LRStateAction; transitionVector: array of LRStateAction); - function ActionCount: integer; - function GetAction(index:integer): LRStateAction; - function GetActionBySymbolIndex(symbolIndex:integer): LRStateAction; - end; - - Rule = class - Index: integer; - NonTerminal: Symbol; - m_symbols: array of Symbol; - ContainsOneNonTerminal: boolean; - m_hasEmpty: boolean; - public - constructor(index:integer; nonTerminal:Symbol; symbols: array of Symbol); - function GetIndex(key:integer):Symbol; - property this[i:integer]:Symbol read GetIndex; default; - function Name:string; - function Count:integer; - function Definition:string; - function ToString:string;override; - end; - - DfaEdge = record - CharSetIndex: integer; - TargetIndex: integer; - end; - - Grammar = class - Name: string; // Name of the grammar - Version: string; // Version of the grammar - Author: string; // Author of the grammar - About: string; // Grammar description - m_startSymbolIndex: integer; // Start symbol index - CaseSensitive: boolean; // Grammar is case sensitive or not - // Tables read from the binary grammar file - m_symbolTable: array of Symbol; // Symbol table - m_charSetTable: array of string; // Charset table - m_ruleTable: array of Rule; // Rule table - m_dfaStateTable: array of DfaState; // DFA state table - m_lrStateTable: array of LRState; // LR state table - // Initial states - m_dfaInitialStateIndex: integer; // DFA initial state index - DfaInitialState: DfaState; // DFA initial state - m_lrInitialState: integer; // LR initial state - // Internal state of grammar parser - m_reader: BinaryReader; // Source of the grammar - m_entryCount: integer; // Number of entries left - m_errorSymbol: Symbol; - EndSymbol: Symbol; - constructor(reader:BinaryReader); - function FileHeader:string; - function StartSymbol:Symbol; - function InitialLRState:LRState; - private - procedure Load; - function ReadNextRecord:integer; - procedure ReadHeader; - procedure ReadTableCounts; - procedure ReadInitialStates; - procedure ReadSymbols; - procedure ReadCharSets; - procedure ReadRules; - procedure ReadDfaStates; - procedure ReadLRStates; - function CreateDfaTransitionVector(edges: array of DfaEdge):ObjectMap; - procedure ReadEmptyEntry; - function ReadStringEntry:string; - function ReadInt16Entry:integer; - function ReadByteEntry:byte; - function ReadBoolEntry:boolean; - function ReadEntryType:integer; - function ReadString:string; - function ReadInt16:integer; - function ReadByte:byte; - function ReadBool:boolean; - procedure OptimizeDfaTransitionVectors; - end; - - xParser = object;//class! - SourceLineReadCallback = procedure(pars: xParser; lineStart,lineLength:integer); - - //internal in Parser - Token = record - m_symbol: Symbol; // Token symbol. - m_text: string; // Token text. - m_start: integer; // Token start stream start. - m_length: integer; // Token length. - m_lineNumber: integer; // Token source line number. (1-based). - m_linePosition: integer;// Token position in source line (1-based). - m_syntaxNode: object; // Syntax node which can be attached to the token. - end; - //internal in Parser - LRStackItem = record - m_token: Token; // Token in the LR stack item. - m_state: LRState; // LR state associated with the item. - m_rule: Rule; // Reference to a grammar rule if the item contains non-terminal. - end; - - LALRParser = class - m_grammar: Grammar; // Grammar of parsed language. - TrimReductions: boolean; // Allowes to minimize reduction tree. - m_textReader: System.IO.TextReader;// Data to parse. - m_buffer: array of char; // Buffer to keep current characters. - m_bufferSize, // Size of the buffer. - m_bufferStartIndex, // Absolute position of buffered first character. - m_charIndex, // Index of character in the buffer. - m_preserveChars, // Number of characters to preserve when buffer is refilled. - m_lineStart, // Relative position of line start to the buffer beginning. - m_lineLength, // Length of current source line. - m_lineNumber, // Current line number. - m_commentLevel: integer; // Keeps stack level for embedded comments - m_commentText: StringBuilder; // Current comment text. - SourceLineReadWenLineRead: SourceLineReadCallback;// Called when line reading finished. - m_token: Token; // Current token - m_inputTokens: array of Token; // Stack of input tokens. - m_inputTokenCount: integer; // How many tokens in the input. - m_lrStack: array of LRStackItem;// Stack of LR states used for LR parsing. - m_lrStackIndex: integer; // Index of current LR state in the LR parsing stack. - m_lrState: LRState; // Current LR state. - m_reductionCount: integer; // Number of items in reduction. It is Undefined if no reducton available. - m_expectedTokens: array of Symbol;// What tokens are expected in case of error? - const MinimumBufferSize = 4096; // Minimum size of char buffer. - const EndOfString:char = char(0);// Designates last string terminator. - const MinimumInputTokenCount = 2;// Minimum input token stack size. - const MinimumLRStackSize = 256; // Minimum size of reduction stack. - const Undefined = -1; // Used for undefined int values. - - - constructor(tr:TextReader;grm:Grammar); - function LanguageGrammar: Grammar; - function TextReader: System.IO.TextReader; - function CharPosition: integer; - function LineNumber: integer; // Gets current line number. It is 1-based. - function LinePosition: integer; // Gets current char position in the current source line. It is 1-based. - function LineText: string; // Gets current source line text. It can be truncated if line is longer than 2048 characters. - function ReadBuffer: integer; // Reads next characters to the buffer. - procedure MoveBy(delta:integer);// Increments current char index by delta character positions. - procedure MoveToLineEnd; // Moves current char pointer to the end of source line. - procedure SetTokenSymbol(value:Symbol); - function GetTokenSymbol: Symbol; - property TokenSymbol:Symbol read GetTokenSymbol write SetTokenSymbol; - function GetTokenText: string; - procedure SetTokenText(text:string); - property TokenText:string read GetTokenText write SetTokenText;// Gets or sets current token text. - function GetTokenSyntaxNode: object; - procedure SetTokenSyntaxNode(text:object); - property TokenSyntaxNode:object read GetTokenSyntaxNode write SetTokenSyntaxNode;// Gets or sets token syntax object associated with the current token or reduction. - function TokenString: string;// Returns string representation of the token. - procedure PushInputToken(sym:Symbol; text:string; syntaxNode:object);// Pushes a token to the input token stack. - function PopInputToken: Symbol; // Pops token from the input token stack. - function ReadToken: Symbol; // Reads next token from the input stream. - procedure DiscardInputToken; // Removes current token and pops next token from the input stack. - function CurrentLRState: LRState; - function ReductionRule: Rule; - function ReductionCount: integer; - function GetReductionSyntaxNode(index:integer):object; - function GetExpectedTokens:array of Symbol; - function Parse: ParseMessage;// Executes next step of parser and returns parser state. - procedure ProcessBlockComment; - function CommentText: string;// Gets current comment text. - function ParseToken: TokenParseResult; - - /// - /// Gets or sets current token position relative to input stream beginning. - /// -// public int TokenCharPosition -// { -// get { return m_token.m_start; } -// set { m_token.m_start = value; } -// } - - /// - /// Gets or sets current token text length. - /// -// public int TokenLength -// { -// get { return m_token.m_length; } -// set { m_token.m_length = value; } -// } - - /// - /// Gets or sets current token line number. It is 1-based. - /// -// public int TokenLineNumber -// { -// get { return m_token.m_lineNumber; } -// set { m_token.m_lineNumber = value; } -// } - - /// - /// Gets or sets current token position in current source line. It is 1-based. - /// -// public int TokenLinePosition -// { -// get { return m_token.m_linePosition; } -// set { m_token.m_linePosition = value; } -// } - - end; - - -var UnassignedValue: Unassigned; - - -implementation - - - -//------------------------------------------------------------------------------ -//Symbol members -//------------------------------------------------------------------------------ -constructor Symbol.Create(index:integer; name:string; symType:integer); -begin - m_quotedChars := '|-+*?()[]{}<>!'; - self.Index := index; - self.Name := name; - self.SymbolType := symType; -end; - -function Symbol.IsQuotedChar(value:char): boolean; -begin - result := m_quotedChars.IndexOf(value) >= 0; -end; - -function Symbol.FormatTerminalSymbol(source:string):string; -var res: StringBuilder; - i: integer; - ch: char; -begin - res := new StringBuilder; - for i:=0 to source.Length-1 do begin//string[0] - ch := source[i]; - if ch = '''' then - res.Append('''') - else - if IsQuotedChar(ch) or (ch = '"') then - res.Append('''' + ch + '''') - else - res.Append(ch); - end; - result := res.ToString; -end; - -function Symbol.ToString:string; -begin - if m_text=nil then - case SymbolType of - SymbolTypeNonTerminal: - m_text := '<' + Name + '>'; - SymbolTypeTerminal: - m_text := FormatTerminalSymbol(Name); - else - m_text := '(' + Name + ')'; - end; - result := m_text; -end; - - -//------------------------------------------------------------------------------ -//MapProvider members -//------------------------------------------------------------------------------ -function MapProvider.GetIndex(key:integer):object; -begin - result := -1; -end; - -function MapProvider.GetEntry(key:integer):Entry; -begin -end; - -procedure MapProvider.Add(key:integer;value:object); -begin -end; - -procedure MapProvider.Remove(key:integer); -begin - raise new System.InvalidOperationException(); -end; - -//------------------------------------------------------------------------------ -//SortedMapProvider members -//------------------------------------------------------------------------------ - -constructor SortedMapProvider.Create(capacity:integer); -begin - SetLength(m_entries, capacity); -end; - -function SortedMapProvider.GetIndex(key:integer):object; //override;//possible error! -var minIndex,maxIndex,midIndex:integer; -begin - minIndex := 0; - maxIndex := m_count - 1; - result := nil; - if (maxIndex >= 0) and (key <= m_lastKey) then begin - while minIndex < maxIndex do begin - midIndex := (maxIndex + minIndex) div 2; - if key <= m_entries[midIndex].Key then - maxIndex := midIndex - else - minIndex := midIndex + 1; - end; - if key = m_entries[minIndex].Key then - result := m_entries[minIndex].Value; - end; -end; - -function SortedMapProvider.GetEntry(key:integer):Entry;//override; -begin - result := m_entries[key]; -end; - -procedure SortedMapProvider.Add(key:integer;value:object);//override; -var found: boolean; - index: integer; - entries: array of Entry; -begin - index := FindInsertIndex(key, found); - if found then begin - m_entries[index].Value := value; - exit; - end; - if m_count >= m_entries.Length then begin - SetLength(entries,m_entries.Length + GROWTH); - System.Array.Copy(m_entries, 0, entries, 0, m_entries.Length); - m_entries := entries; - end; - if index < m_count then - System.Array.Copy(m_entries, index, m_entries, index + 1, m_count - index) - else - m_lastKey := key; - m_entries[index].Key := key; - m_entries[index].Value := value; - m_count := m_count + 1; -end; - -procedure SortedMapProvider.Remove(key:integer);//virtual; -var index,tailSize: integer; -begin - index := FindIndex(key); - if index >= 0 then begin - tailSize := (m_count - 1) - index; - if tailSize > 0 then begin - System.Array.Copy(m_entries, index + 1, m_entries, index, tailSize); - end else - if m_count > 1 then begin - m_lastKey := m_entries[m_count - 2].Key; - end else begin - m_lastKey := INVALIDKEY; - end; - m_count := m_count - 1; - m_entries[m_count].Key := INVALIDKEY; - m_entries[m_count].Value := nil; - end; -end; - -function SortedMapProvider.FindIndex(key:integer):integer; //possible error! -var minIndex,maxIndex,midIndex: integer; -begin - minIndex := 0; - result := -1; - if (m_count > 0) and (key <= m_lastKey) then begin - maxIndex := m_count - 1; - while minIndex < maxIndex do begin - midIndex := (maxIndex + minIndex) div 2; - if key <= m_entries[midIndex].Key then - maxIndex := midIndex - else - minIndex := midIndex + 1; - end; - if key = m_entries[minIndex].Key then - result := minIndex; - end; -end; - -function SortedMapProvider.FindInsertIndex(key:integer;var found:boolean):integer;//possible error! -var minIndex,maxIndex,midIndex: integer; -begin - minIndex := 0; - found := false; - result := m_count; - if (m_count > 0) and (key <= m_lastKey) then begin - maxIndex := m_count - 1; - while minIndex < maxIndex do begin - midIndex := (maxIndex + minIndex) div 2; - if key <= m_entries[midIndex].Key then - maxIndex := midIndex - else - minIndex := midIndex + 1; - end; - found := key = m_entries[minIndex].Key; - result := minIndex; - end; -end; - -//------------------------------------------------------------------------------ -//IndexMapProvider members -//------------------------------------------------------------------------------ -constructor IndexMapProvider.Create; -var i: integer; -begin - SetLength(m_array,MAXINDEX + 1); - for i:=0 to m_array.Length-1 do - m_array[i] := UnassignedValue; -end; - -function IndexMapProvider.GetIndex(key:integer):object; //override; -begin - if (key >= m_array.Length) or (key < 0) then - result := nil - else - result := m_array[key]; -end; - -function IndexMapProvider.GetEntry(key:integer):Entry;//override; -var idx,i: integer; - value: object; - entr: Entry; -begin - idx := -1; - result := entr; - for i := 0 to m_array.Length-1 do begin - value := m_array[i]; - if value <> UnassignedValue then - idx := idx +1; - if idx = key then begin - entr.key := i; - entr.value := value; - exit; - end; - end; -end; - -procedure IndexMapProvider.Add(key:integer;value:object);//override; -begin - m_array[key] := value; - m_count := m_count + 1; -end; - - -//------------------------------------------------------------------------------ -//ArrayMapProvider members -//------------------------------------------------------------------------------ -constructor ArrayMapProvider.Create(capacity:integer); -begin - SetLength(m_entries,capacity); -end; - -function ArrayMapProvider.GetIndex(key:integer):object; //override; -var i,entryKey:integer; - entr: Entry; -begin - i := m_count; - result := nil; - while i>0 do begin - i := i - 1; - entr := m_entries[i]; - entryKey := entr.Key; - if entryKey < key then begin - continue; - end else - if entryKey = key then begin - result := entr.Value; - break; - end else - if entryKey > key then begin - result := nil; - break; - end; - end; -end; - -function ArrayMapProvider.GetEntry(key:integer):Entry;//override; -begin - result := m_entries[key]; -end; - -procedure ArrayMapProvider.Add(key:integer;value:object);//override; -begin - m_entries[m_count].Key := key; - m_entries[m_count].Value := value; - m_count := m_count +1; -end; - - -//------------------------------------------------------------------------------ -//ObjectMap members -//------------------------------------------------------------------------------ -constructor ObjectMap.Create; -begin - m_mapProvider := new SortedMapProvider(MINSIZE); -end; - -function ObjectMap.getCount:integer; -begin - result := m_mapProvider.m_count; -end; - -procedure ObjectMap.SetReadOnly(value: boolean); -begin - if m_readonly<>value then - SetMapProvider(value); - m_readonly := value; -end; - -function ObjectMap.GetIndex(key:integer):object; -begin - result := m_mapProvider[key]; -end; - -procedure ObjectMap.SetIndex(key:integer; value:object); -begin - m_mapProvider.Add(key, value); -end; - -function ObjectMap.GetKey(index:integer):integer; -begin - result := m_mapProvider.GetEntry(index).Key; -end; - -procedure ObjectMap.Remove(key:integer); -begin - m_mapProvider.Remove(key); -end; - -procedure ObjectMap.Add(key:integer;value:object); -begin - m_mapProvider.Add(key, Entry(value)); -end; - -procedure ObjectMap.SetMapProvider(readOnly:boolean); -var i,count: integer; - provider: MapProvider; - pr: SortedMapProvider; - entr:Entry; -begin - count := m_mapProvider.m_count; - provider := m_mapProvider; - if readOnly then begin - pr := m_mapProvider as SortedMapProvider; - if pr.m_lastKey <= MAXINDEX then begin - provider := new IndexMapProvider; - end else - if (count <= MAXARRAYCOUNT) then begin - provider := new ArrayMapProvider(m_mapProvider.m_count); - end; - end else begin - if not (provider is SortedMapProvider) then - provider := new SortedMapProvider(m_mapProvider.m_count); - end; - if provider <> m_mapProvider then begin - for i:=0 to count-1 do begin - entr := m_mapProvider.GetEntry(i); - provider.Add(entr.Key, entr.Value); - end; - m_mapProvider := provider; - end; -end; - - -//------------------------------------------------------------------------------ -//DFAState members -//------------------------------------------------------------------------------ -constructor DFAState.Create(Index:integer;acceptSymbol:Symbol;transitionVector:ObjectMap); -begin - self.Index := Index; - self.AcceptSymbol := AcceptSymbol; - m_transitionVector := transitionVector; -end; - - -//------------------------------------------------------------------------------ -//LRStateAction members -//------------------------------------------------------------------------------ -constructor LRStateAction.Create(Index:integer;StateSymbol:Symbol;Action:integer;Value:integer); -begin - self.Index := Index; - self.StateSymbol := StateSymbol; - self.Action := Action; - self.Value := Value; -end; - - -//------------------------------------------------------------------------------ -//LRState members -//------------------------------------------------------------------------------ -constructor LRState.Create(Index:integer; actions: array of LRStateAction; transitionVector: array of LRStateAction); -begin - self.Index := index; - m_actions := actions; - m_transitionVector := transitionVector; -end; - -function LRState.ActionCount: integer; -begin - result := m_actions.Length; -end; - -function LRState.GetAction(index:integer): LRStateAction; -begin - result := m_actions[index]; -end; - -function LRState.GetActionBySymbolIndex(symbolIndex:integer): LRStateAction; -begin - result := m_transitionVector[symbolIndex]; -end; - - -//------------------------------------------------------------------------------ -//Rule members -//------------------------------------------------------------------------------ -constructor Rule.Create(index:integer; nonTerminal:Symbol; symbols: array of Symbol); -begin - self.Index := Index; - self.NonTerminal := nonTerminal; - m_symbols := symbols; - ContainsOneNonTerminal := (symbols.Length = 1) and (symbols[0].SymbolType = SymbolTypeNonTerminal); - m_hasEmpty := symbols.Length = 0;// && m_nonTerminal.Name == "empty"; -end; - -function Rule.Name:string; -begin - result := '<' + NonTerminal.Name + '>'; -end; - -function Rule.Count:integer; -begin - result := m_symbols.Length; -end; - -function Rule.GetIndex(key:integer):Symbol; -begin - result := m_symbols[index]; -end; - -function Rule.Definition:string; -var res:StringBuilder; - i:integer; -begin - res := new StringBuilder; - for i:=0 to m_symbols.Length-1 do begin - res.Append(m_symbols[i].ToString); - if i < m_symbols.Length - 1 then - res.Append(' '); - end; - result := res.ToString; -end; - -function Rule.ToString:string; -begin - result := Name + ' ::= ' + Definition; -end; - - -//------------------------------------------------------------------------------ -//Grammar members -//------------------------------------------------------------------------------ -constructor Grammar.Create(reader:BinaryReader); -begin - if reader = nil then - raise new System.ArgumentNullException('reader'); - m_reader := reader; - Load; -end; - -function Grammar.FileHeader:string; -begin - result := 'GOLD Parser Tables/v1.0'; -end; -function Grammar.StartSymbol:Symbol; -begin - result := m_symbolTable[m_startSymbolIndex]; -end; -function Grammar.InitialLRState:LRState; -begin - result := m_lrStateTable[m_lrInitialState]; -end; - - -procedure Grammar.Load; -var recordType: integer; -begin - if FileHeader<>ReadString then - raise new FileLoadException('GPBLALRParser.GrammarWrongFileHeader'); - while m_reader.PeekChar <> -1 do begin - recordType := ReadNextRecord; - case recordType of - RecordTypeParameters: ReadHeader; - RecordTypeTableCounts: ReadTableCounts; - RecordTypeInitial: ReadInitialStates; - RecordTypeSymbols: ReadSymbols; - RecordTypeCharSets: ReadCharSets; - RecordTypeRules: ReadRules; - RecordTypeDfaStates: ReadDfaStates; - RecordTypeLRStates: ReadLRStates; - else - raise new FileLoadException('GPBLALRParser.GrammarInvalidRecordType'); - end; - end; - DfaInitialState := m_dfaStateTable[m_dfaInitialStateIndex]; - OptimizeDfaTransitionVectors; -end; - -function Grammar.ReadNextRecord:integer; -var recordType:char; -begin - recordType := char(m_reader.ReadByte); - case recordType of - 'M': begin - m_entryCount := ReadInt16; - result := ReadByteEntry; - end; - else - raise new FileLoadException('GPBLALRParser.Grammar_InvalidRecordHeader'); - end; -end; - -procedure Grammar.ReadHeader; -begin - Name := ReadStringEntry; - Version := ReadStringEntry; - Author := ReadStringEntry; - About := ReadStringEntry; - CaseSensitive := ReadBoolEntry; - m_startSymbolIndex := ReadInt16Entry; -end; - -procedure Grammar.ReadTableCounts; -begin - SetLength(m_symbolTable ,ReadInt16Entry); - SetLength(m_charSetTable ,ReadInt16Entry); - SetLength(m_ruleTable ,ReadInt16Entry); - SetLength(m_dfaStateTable ,ReadInt16Entry); - SetLength(m_lrStateTable ,ReadInt16Entry); -end; - -procedure Grammar.ReadInitialStates; -begin - m_dfaInitialStateIndex := ReadInt16Entry; - m_lrInitialState := ReadInt16Entry; -end; - -procedure Grammar.ReadSymbols; -var index: integer; - name: string; - symbolType: integer; - sym: Symbol; -begin - index := ReadInt16Entry; - name := ReadStringEntry; - symbolType := ReadInt16Entry; - sym := new Symbol(index, name, symbolType); - case symbolType of - SymbolTypeError: m_errorSymbol := sym; - SymbolTypeEnd: EndSymbol := sym; - end; - m_symbolTable[index] := sym; -end; - -procedure Grammar.ReadCharSets; -begin - m_charSetTable[ReadInt16Entry] := ReadStringEntry; -end; - -procedure Grammar.ReadRules; -var i,index: integer; - nonTerminal: Symbol; - symbols: array of Symbol; -begin - index := ReadInt16Entry; - nonTerminal := m_symbolTable[ReadInt16Entry]; - ReadEmptyEntry; - SetLength(symbols,m_entryCount); - for i:=0 to symbols.Length-1 do - symbols[i] := m_symbolTable[ReadInt16Entry]; - m_ruleTable[index] := new Rule(index, nonTerminal, symbols); -end; - -procedure Grammar.ReadDfaStates; -var i,index: integer; - acceptSymbol: Symbol; - acceptState: boolean; - edges: array of DfaEdge; - transitionVector: ObjectMap; -begin - index := ReadInt16Entry; - acceptSymbol := nil; - acceptState := ReadBoolEntry; - if acceptState then - acceptSymbol := m_symbolTable[ReadInt16Entry] - else - ReadInt16Entry; - ReadEmptyEntry; - // Read DFA edges - SetLength(edges,m_entryCount div 3); - for i:=0 to edges.Length-1 do begin - edges[i].CharSetIndex := ReadInt16Entry; - edges[i].TargetIndex := ReadInt16Entry; - ReadEmptyEntry; - end; - // Create DFA state and store it in DFA state table - transitionVector := CreateDfaTransitionVector(edges); - m_dfaStateTable[index] := new DfaState(index, acceptSymbol, transitionVector); -end; - -procedure Grammar.ReadLRStates; -var targetIndex,i,index: integer; - stateTable: array of LRStateAction; - transitionVector: array of LRStateAction; - sym: Symbol; - action: integer; -begin - index := ReadInt16Entry; - ReadEmptyEntry; - SetLength(stateTable, m_entryCount div 4); - for i:=0 to stateTable.Length-1 do begin - sym := m_symbolTable[ReadInt16Entry]; - action := ReadInt16Entry; - targetIndex := ReadInt16Entry; - ReadEmptyEntry(); - stateTable[i] := new LRStateAction(i, sym, action, targetIndex); - end; - // Create the transition vector - SetLength(transitionVector,m_symbolTable.Length); - for i:=0 to transitionVector.Length-1 do - transitionVector[i] := nil; - for i:=0 to stateTable.Length-1 do - transitionVector[stateTable[i].StateSymbol.Index] := stateTable[i]; - m_lrStateTable[index] := new LRState(index, stateTable, transitionVector); -end; - -function Grammar.CreateDfaTransitionVector(edges: array of DfaEdge):ObjectMap; -var i,j: integer; - charSet: string; -begin - result := new ObjectMap; - i := edges.Length; - while i > 0 do begin - i := i - 1; - charSet := m_charSetTable[edges[i].CharSetIndex]; - for j:=0 to charSet.Length-1 do - result[integer(charSet[j])] := edges[i].TargetIndex; - end; -end; - -procedure Grammar.ReadEmptyEntry; -begin - if ReadEntryType<>EntryTypeEmpty then - raise new FileLoadException('GPBLALRParser.GrammarEmptyEntryExpected'); - m_entryCount := m_entryCount - 1; -end; - -function Grammar.ReadStringEntry:string; -begin - if ReadEntryType<>EntryTypeString then - raise new FileLoadException('GPBLALRParser.GrammarStringEntryExpected'); - m_entryCount := m_entryCount - 1; - result := ReadString; -end; - -function Grammar.ReadInt16Entry:integer; -begin - if ReadEntryType<>EntryTypeInteger then - raise new FileLoadException('GPBLALRParser.GrammarIntegerEntryExpected'); - m_entryCount := m_entryCount - 1; - result := ReadInt16; -end; - -function Grammar.ReadByteEntry:byte; -begin - if ReadEntryType<>EntryTypeByte then - raise new FileLoadException('GPBLALRParser.GrammarByteEntryExpected'); - m_entryCount := m_entryCount - 1; - result := ReadByte; -end; - -function Grammar.ReadBoolEntry:boolean; -begin - if ReadEntryType<>EntryTypeBoolean then - raise new FileLoadException('GPBParser.GrammarBooleanEntryExpected'); - m_entryCount := m_entryCount - 1; - result := ReadBool; -end; - -function Grammar.ReadEntryType:integer; -begin - if m_entryCount = 0 then - raise new FileLoadException('GPBLALRParser.GrammarNoEntry'); - result := ReadByte; -end; - -function Grammar.ReadString:string; -var res: StringBuilder; - unicodeChar: char; -begin - res := new StringBuilder; - unicodeChar := char(integer(ReadInt16)); - while unicodeChar <> char(0) do begin - res.Append(unicodeChar); - unicodeChar := char(integer(ReadInt16)); - end; - result := res.ToString; -end; - -function Grammar.ReadInt16:integer; -begin - result := m_reader.ReadUInt16; -end; - -function Grammar.ReadByte:byte; -begin - result := m_reader.ReadByte; -end; - -function Grammar.ReadBool:boolean; -begin - result := ReadByte = 1; -end; - -procedure Grammar.OptimizeDfaTransitionVectors; -var dfaStates: array of DfaState; - state: DfaState; - transitions: ObjectMap; - transition: object; - key,i,j,transitionIndex: integer; -begin - dfaStates := m_dfaStateTable; - for j:=0 to dfaStates.Length-1 do begin - state := dfaStates[j]; - transitions := state.m_transitionVector; - for i:=transitions.Count-1 downto 0 do begin - key := transitions.GetKey(i); - transition := transitions[key]; - if transition <> nil then begin - transitionIndex := integer(transition); - if transitionIndex >= 0 then - transitions[key] := dfaStates[transitionIndex] - else - transitions[key] := nil; - end; - end; - transitions.ReadOnly := true; - end; -end; - - - -//------------------------------------------------------------------------------ -//Parser members -//------------------------------------------------------------------------------ -constructor LALRParser.Create(tr:System.IO.TextReader; grm:Grammar); -var start: LRStackItem; -begin - m_lineNumber := 1; - TrimReductions := true; - if tr = nil then - raise new ArgumentNullException('textReader'); - if grm = nil then - raise new ArgumentNullException('grammar'); - m_textReader := tr; - m_bufferSize := MinimumBufferSize; - SetLength(m_buffer,m_bufferSize + 1); - m_lineLength := Undefined; - ReadBuffer; - SetLength(m_inputTokens,MinimumInputTokenCount); - SetLength(m_lrStack,MinimumLRStackSize); - m_grammar := grm; - // Put grammar start symbol into LR parsing stack. - m_lrState := m_grammar.InitialLRState; - start.m_token.m_symbol := m_grammar.StartSymbol; - start.m_state := m_lrState; - m_lrStack[m_lrStackIndex] := start; - m_reductionCount := Undefined; // there are no reductions yet. -end; - -function LALRParser.LanguageGrammar: Grammar; -begin - result := m_grammar; -end; - -function LALRParser.TextReader: System.IO.TextReader; -begin - result := m_textReader; -end; - -function LALRParser.CharPosition: integer; -begin - result := m_charIndex + m_bufferStartIndex; -end; - -function LALRParser.LineNumber: integer;// Gets current line number. It is 1-based. -begin - result := m_lineNumber; -end; - -function LALRParser.LinePosition:integer;// Gets current char position in the current source line. It is 1-based. -begin - result := CharPosition - m_lineStart + 1; -end; - -function LALRParser.LineText: string; // Gets current source line text. It can be truncated if line is longer than 2048 characters. -var lineStart,lineLength: integer; -begin - lineStart := Math.Max(m_lineStart, 0); - if m_lineLength = Undefined then - // Line was requested outside of SourceLineReadCallback call - lineLength := m_charIndex - lineStart - else - lineLength := m_lineLength - (lineStart - m_lineStart); - if lineLength > 0 then - result := new String(m_buffer, lineStart, lineLength) - else - result := string.Empty; -end; - -function LALRParser.ReadBuffer: integer; // Reads next characters to the buffer. -var lineStart,lineCharCount,moveIndex,moveCount,count:integer; - newBuffer:array of char; -begin - // Find out how many bytes to preserve. - // We truncate long lines. - lineStart := m_lineStart; - if m_lineStart < 0 then - lineStart := 0; - lineCharCount := m_charIndex - lineStart; - if lineCharCount > m_bufferSize div 2 then - lineCharCount := m_bufferSize div 2; - moveIndex := m_charIndex - lineCharCount; - moveCount := lineCharCount + m_preserveChars; - if moveCount > 0 then - // We need to keep current token characters. - if m_bufferSize - moveCount < 20 then begin - // Grow the buffer - m_bufferSize := m_bufferSize * 2; - SetLength(newBuffer,m_bufferSize + 1); - System.Array.Copy(m_buffer, moveIndex, newBuffer, 0, moveCount); - m_buffer := newBuffer; - end else begin - System.Array.Copy(m_buffer, moveIndex, m_buffer, 0, moveCount); - end; - // Read as many characters as possible. - count := m_bufferSize - moveCount; - result := m_textReader.Read(m_buffer, moveCount, count); - // Mark character after last read one as End-Of-String - m_buffer[moveCount + result] := EndOfString; - // Adjust buffer variables. - m_bufferStartIndex := m_bufferStartIndex + moveIndex; - m_charIndex := m_charIndex - moveIndex; - m_lineStart := m_lineStart - moveIndex; -end; - -procedure LALRParser.MoveBy(delta:integer);// Increments current char index by delta character positions. -var i,lastIndex:integer; -begin - for i:=delta-1 downto 0 do begin - m_charIndex := m_charIndex + 1; - if m_buffer[m_charIndex-1] = charEnter then begin// '\n' - if SourceLineReadWenLineRead <> nil then begin - m_lineLength := m_charIndex - m_lineStart - 1; // Exclude '\n' - lastIndex := m_lineStart + m_lineLength - 1; - if (lastIndex >= 0) and (m_buffer[lastIndex] = charReset) then //'\r' - m_lineLength := m_lineLength - 1; - if m_lineLength < 0 then - m_lineLength := 0; - SourceLineReadWenLineRead(self, m_lineStart + m_bufferStartIndex, m_lineLength); - end; - m_lineNumber := m_lineNumber + 1; - m_lineStart := m_charIndex; - m_lineLength := Undefined; - end; - if m_buffer[m_charIndex] = charEndOfFile then - if SourceLineReadWenLineRead <> nil then begin - m_lineLength := m_charIndex - m_lineStart; - if m_lineLength > 0 then - SourceLineReadWenLineRead(self, m_lineStart + m_bufferStartIndex, m_lineLength); - m_lineLength := Undefined; - end; - end; -end; - -procedure LALRParser.MoveToLineEnd; // Moves current char pointer to the end of source line. -var ch: char; -begin - while true do begin - ch := m_buffer[m_charIndex]; - case ch of - charEnter,charReset: - break; - charEndOfFile: - if ReadBuffer = 0 then - break; - else - if m_commentText <> nil then - m_commentText.Append(ch); - end; - m_charIndex := m_charIndex + 1; - end; -end; - -procedure LALRParser.SetTokenSymbol(value:Symbol); -begin - m_token.m_symbol := value; -end; -function LALRParser.GetTokenSymbol: Symbol; -begin - result := m_token.m_symbol; -end; - -function LALRParser.GetTokenText: string; -begin - if m_token.m_text = nil then begin - if m_token.m_length > 0 then - m_token.m_text := new string(m_buffer, m_token.m_start - m_bufferStartIndex, m_token.m_length) - else - m_token.m_text := string.Empty; - end; - result := m_token.m_text; -end; - -procedure LALRParser.SetTokenText(text:string); -begin - m_token.m_text := text; -end; - -function LALRParser.GetTokenSyntaxNode: object; -begin - if m_reductionCount = Undefined then - result := m_token.m_syntaxNode - else - result := m_lrStack[m_lrStackIndex].m_token.m_syntaxNode; -end; - -procedure LALRParser.SetTokenSyntaxNode(text:object); -begin - if m_reductionCount = Undefined then - m_token.m_syntaxNode := text - else - m_lrStack[m_lrStackIndex].m_token.m_syntaxNode := text; -end; - -function LALRParser.TokenString: string;// Returns string representation of the token. -var sb: StringBuilder; - i: integer; - ch: char; -begin - if m_token.m_symbol.SymbolType <> SymbolTypeTerminal then begin - result := m_token.m_symbol.ToString; - exit; - end; - sb := new StringBuilder(m_token.m_length); - for i := 0 to m_token.m_length-1 do begin - ch := m_buffer[m_token.m_start - m_bufferStartIndex + i]; - if ch < ' ' then - case ch of - charEnter: sb.Append('{LF}'); - charReset: sb.Append('{CR}'); - charTab: sb.Append('{HT}'); - end - else - sb.Append(ch); - end; - result := sb.ToString;{} -end; - -procedure LALRParser.PushInputToken(sym:Symbol; text:string; syntaxNode:object);// Pushes a token to the input token stack. -var newTokenArray: array of Token; -begin - if m_token.m_symbol <> nil then begin - if m_inputTokenCount = m_inputTokens.Length then begin - SetLength(newTokenArray, m_inputTokenCount*2); - System.Array.Copy(m_inputTokens, newTokenArray, m_inputTokenCount); - m_inputTokens := newTokenArray; - end; - m_inputTokens[m_inputTokenCount] := m_token; - m_inputTokenCount := m_inputTokenCount + 1; - end; -// m_token := new Token; - m_token.m_symbol := sym; - m_token.m_text := text; - m_token.m_length := 0; - if text <> nil then - m_token.m_length := text.Length; - m_token.m_syntaxNode := syntaxNode;{} -end; - -function LALRParser.PopInputToken: Symbol; // Pops token from the input token stack. -begin - result := m_token.m_symbol; - if m_inputTokenCount > 0 then begin - m_inputTokenCount := m_inputTokenCount - 1; - m_token := m_inputTokens[m_inputTokenCount]; - end else begin - m_token.m_symbol := nil; - m_token.m_text := nil; - end;{} -end; - - - -function LALRParser.ReadToken: Symbol; // Reads next token from the input stream. -var tokenSymbol: Symbol; - dfaStateTable: array of DfaState; - ch: char; - curDFAState: DfaState; - lookahead,tokenLength: integer; -begin - m_token.m_text := nil; - m_token.m_start := m_charIndex + m_bufferStartIndex; - m_token.m_lineNumber := m_lineNumber; - m_token.m_linePosition := m_charIndex - m_lineStart + 1; - lookahead := m_charIndex; // Next look ahead char in the input - tokenLength := 0; - tokenSymbol := nil; - dfaStateTable := m_grammar.m_dfaStateTable; - ch := m_buffer[lookahead]; - if ch = EndOfString then begin - if ReadBuffer = 0 then begin - m_token.m_symbol := m_grammar.EndSymbol; - m_token.m_length := 0; - result := m_token.m_symbol; - exit; - end; - lookahead := m_charIndex; - ch := m_buffer[lookahead]; - end; - curDFAState := m_grammar.DfaInitialState; - while true do begin - curDFAState := curDFAState.m_transitionVector[integer(ch)] as DfaState; - // This block-if statement checks whether an edge was found from the current state. - // If so, the state and current position advance. Otherwise it is time to exit the main loop - // and report the token found (if there was it fact one). If the LastAcceptState is -1, - // then we never found a match and the Error Token is created. Otherwise, a new token - // is created using the Symbol in the Accept State and all the characters that - // comprise it. - if curDFAState <> nil then begin - // This code checks whether the target state accepts a token. If so, it sets the - // appropiate variables so when the algorithm in done, it can return the proper - // token and number of characters. - lookahead := lookahead + 1; - if curDFAState.AcceptSymbol <> nil then begin - tokenSymbol := curDFAState.AcceptSymbol; - tokenLength := lookahead - m_charIndex; - end; - ch := m_buffer[lookahead]; - if ch = EndOfString then begin - m_preserveChars := lookahead - m_charIndex; - if ReadBuffer = 0 then - lookahead := m_charIndex + m_preserveChars - else begin - lookahead := m_charIndex + m_preserveChars; - ch := m_buffer[lookahead]; - end; - m_preserveChars := 0; - end; - end else begin - if tokenSymbol <> nil then begin - m_token.m_symbol := tokenSymbol; - m_token.m_length := tokenLength; - MoveBy(tokenLength); - end else begin - //Tokenizer cannot recognize symbol - m_token.m_symbol := m_grammar.m_errorSymbol; - m_token.m_length := 1; - MoveBy(1); - end; - break; - end; - end; - result := m_token.m_symbol;{} -end; - -procedure LALRParser.DiscardInputToken; // Removes current token and pops next token from the input stack. -begin - if m_inputTokenCount > 0 then begin - m_inputTokenCount := m_inputTokenCount - 1; - m_token := m_inputTokens[m_inputTokenCount]; - end else begin - m_token.m_symbol := nil; - m_token.m_text := nil; - end;{} -end; - -function LALRParser.CurrentLRState: LRState; -begin - result := m_lrState; -end; - -function LALRParser.ReductionRule: Rule; -begin - result := m_lrStack[m_lrStackIndex].m_rule; -end; - -function LALRParser.ReductionCount: integer; -begin - result := m_reductionCount; -end; - -function LALRParser.GetReductionSyntaxNode(index:integer):object; -begin - //DarkStar Changed for Speed! - //if (index < 0 || index >= m_reductionCount) - // throw new IndexOutOfRangeException(); - result := m_lrStack[m_lrStackIndex - m_reductionCount + index].m_token.m_syntaxNode;{} -end; - -function LALRParser.GetExpectedTokens:array of Symbol; -begin - result := m_expectedTokens; {} -end; - -function LALRParser.Parse: ParseMessage;// Executes next step of parser and returns parser state. -var parseResult: TokenParseResult; - readTokenSymbol: Symbol; - symType: integer; -begin - if m_token.m_symbol <> nil then - case m_token.m_symbol.SymbolType of - SymbolTypeCommentLine:begin - DiscardInputToken; //Remove it - MoveToLineEnd; - end; - SymbolTypeCommentStart: - ProcessBlockComment; - end; - while true do begin - if m_token.m_symbol = nil then begin - //We must read a token - readTokenSymbol := ReadToken; - symType := readTokenSymbol.SymbolType; - if (m_commentLevel = 0) and (symType <> SymbolTypeCommentLine) and (symType <> SymbolTypeCommentStart) and (symType <> SymbolTypeWhiteSpace) then begin - result := ParseMessageTokenRead; - exit; - end; - end else begin - //==== Normal parse mode - we have a token and we are not in comment mode - case m_token.m_symbol.SymbolType of - SymbolTypeWhiteSpace: - DiscardInputToken; // Discard Whitespace - SymbolTypeCommentStart:begin - m_commentLevel := 1; // Switch to block comment mode. - result := ParseMessageCommentBlockRead; - exit; - end; - SymbolTypeCommentLine:begin - result := ParseMessageCommentLineRead; - exit; - end; - SymbolTypeError:begin - result := ParseMessageLexicalError; - exit; - end; - else begin - //Finally, we can parse the token - parseResult := ParseToken; - case parseResult of - TokenParseResultAccept:begin - result := ParseMessageAccept; - exit; - end; - TokenParseResultInternalError:begin - result := ParseMessageInternalError; - exit; - end; - TokenParseResultReduceNormal:begin - if not m_lrStack[m_lrStackIndex].m_rule.m_hasEmpty then begin - result := ParseMessageReduction; - exit; - end; - end; - TokenParseResultShift: - //A simple shift, we must continue - DiscardInputToken; // Okay, remove the top token, it is on the stack - TokenParseResultSyntaxError:begin - result := ParseMessageSyntaxError; - exit; - end; - end; - end; - end; - end; - end; -end; - -procedure LALRParser.ProcessBlockComment; -var symType: integer; -begin - if m_commentLevel > 0 then begin - if m_commentText <> nil then - m_commentText.Append(TokenText); - DiscardInputToken(); - while true do begin - symType := ReadToken.SymbolType; - if m_commentText <> nil then - m_commentText.Append(TokenText); - DiscardInputToken(); - case symType of - SymbolTypeCommentStart: - m_commentLevel := m_commentLevel + 1; - SymbolTypeCommentEnd:begin - m_commentLevel := m_commentLevel - 1; - if m_commentLevel = 0 then - exit; - end; - SymbolTypeEnd: - raise new Exception('CommentError'); - end; - end; - end;{} -end; - -function LALRParser.CommentText: string;// Gets current comment text. -var lineComment,blockComment:string; -begin - result := string.Empty; - if m_token.m_symbol <> nil then - case m_token.m_symbol.SymbolType of - SymbolTypeCommentLine:begin - m_commentText := new StringBuilder; - m_commentText.Append(TokenText); - DiscardInputToken; //Remove token - MoveToLineEnd; - lineComment := m_commentText.ToString; - m_commentText := nil; - result := lineComment; - end; - SymbolTypeCommentStart:begin - m_commentText := new StringBuilder; - ProcessBlockComment; - blockComment := m_commentText.ToString; - m_commentText := nil; - result := blockComment; - end; - end;{} -end; - - -function LALRParser.ParseToken: TokenParseResult; -var stateAction: LRStateAction; - length,i,newIndex: integer; - newArray: array of Symbol; - nextToken:LRStackItem; - currentRule: Rule; - head: LRStackItem; - nextState: LRState; -begin - stateAction := m_lrState.m_transitionVector[m_token.m_symbol.Index]; - if stateAction <> nil then begin - //Work - shift or reduce - if m_reductionCount > 0 then begin - newIndex := m_lrStackIndex - m_reductionCount; - m_lrStack[newIndex] := m_lrStack[m_lrStackIndex]; - m_lrStackIndex := newIndex; - end; - m_reductionCount := Undefined; - case stateAction.Action of - LRActionAccept:begin - m_reductionCount := 0; - result := TokenParseResultAccept; - exit; - end; - LRActionShift:begin - m_lrState := m_grammar.m_lrStateTable[stateAction.Value]; - //nextToken := new LRStackItem; - nextToken.m_token := m_token; - nextToken.m_state := m_lrState; - m_lrStackIndex := m_lrStackIndex + 1; - m_lrStack[m_lrStackIndex] := nextToken; - result := TokenParseResultShift; - exit; - end; - LRActionReduce:begin - //Produce a reduction - remove as many tokens as members in the rule & push a nonterminal token - currentRule := m_grammar.m_ruleTable[stateAction.Value]; - if TrimReductions and currentRule.ContainsOneNonTerminal then begin - head := m_lrStack[m_lrStackIndex]; - head.m_token.m_symbol := currentRule.NonTerminal; - head.m_token.m_text := nil; - nextState := m_lrStack[m_lrStackIndex - 1].m_state; - m_lrState := m_grammar.m_lrStateTable[nextState.m_transitionVector[currentRule.NonTerminal.Index].Value]; - head.m_state := m_lrState; - m_lrStack[m_lrStackIndex] := head; - result := TokenParseResultReduceEliminated; - exit; - end else begin - //head := new LRStackItem; - head.m_rule := currentRule; - head.m_token.m_symbol := currentRule.NonTerminal; - head.m_token.m_text := nil; - m_reductionCount := currentRule.m_symbols.Length; - nextState := m_lrStack[m_lrStackIndex - m_reductionCount].m_state; - m_lrState := m_grammar.m_lrStateTable[nextState.m_transitionVector[currentRule.NonTerminal.Index].Value]; - head.m_state := m_lrState; - m_lrStackIndex := m_lrStackIndex + 1; - m_lrStack[m_lrStackIndex] := head; - result := TokenParseResultReduceNormal; - exit; - end; - end; - end; - end; - //=== Syntax Error! Fill Expected Tokens - SetLength(m_expectedTokens, m_lrState.ActionCount); - length := 0; - for i := 0 to m_lrState.ActionCount-1 do - case m_lrState.GetAction(i).StateSymbol.SymbolType of - SymbolTypeTerminal,SymbolTypeEnd:begin - m_expectedTokens[length] := m_lrState.GetAction(i).StateSymbol; - length := length + 1; - end; - end; - if length < m_expectedTokens.Length then begin - SetLength(newArray, length); - System.Array.Copy(m_expectedTokens, newArray, length); - m_expectedTokens := newArray; - end; - result := TokenParseResultSyntaxError;{} -end; -{} -initialization - UnassignedValue := new Unassigned; -finalization - -end. \ No newline at end of file diff --git a/bin/Lib/MathForKumir.pas b/bin/Lib/MathForKumir.pas deleted file mode 100644 index 867c67ec9..000000000 --- a/bin/Lib/MathForKumir.pas +++ /dev/null @@ -1,32 +0,0 @@ -unit MathForKumir; -interface - function ctg(x:real):real; - function arcctg(x:real):real; - function md (x,y:integer):integer; - function dv(x,y:integer):integer; - function Power(x,y:real):real; - -implementation - function ctg(x:real):real; - begin - result:=1/tan(x); - end; - function arcctg(x:real):real; - begin - result:=Pi/2-arctan(x);; - end; - function md (x,y:integer):integer; - begin - result:=x mod y; - - end; - function dv(x,y:integer):integer; - begin - result:=x div y; - end; - function Power(x,y:real):real; - begin - result:=Exp(y*Ln(x)); - end; - -end. \ No newline at end of file diff --git a/bin/Lng/Eng/SemanticErrors_ib.dat b/bin/Lng/Eng/SemanticErrors_ib.dat index ce30b545a..137f6e6a4 100644 --- a/bin/Lng/Eng/SemanticErrors_ib.dat +++ b/bin/Lng/Eng/SemanticErrors_ib.dat @@ -117,7 +117,8 @@ EXPLICIT_IMPLEMENTATION_EXPECTED=Method implementation expected ANONYMOUS_FUNCTION_TYPE_WITH_GENERICS=Anonymous delegates can not use generic parameters CLASS_NOT_IMPLEMENT_THIS_INTERFACE=Class doesn't implement this interface INTERFACE_EXPECTED=Interface expected -IMPLICIT_EXPLICIT_OPERATOR_EXTENSION_ONLY_FOR_COMPILED_CLASSES_ALLOWED=Operators implicit and explicit can be extended only for classes from another .NET assemblies +IMPLICIT_EXPLICIT_OPERATOR_EXTENSION_ONLY_FOR_COMPILED_CLASSES_ALLOWED=Operators implicit and explicit can be extended only for classes from another .NET assemblies +EXTENSIONMETHOD_KEYWORD_NOT_ALLOWED=extensionmethod is not allowed in this context %PREFIX%=COMPILATIONERROR_ UNIT_MODULE_EXPECTED_LIBRARY_FOUND=Unit expected, library found ASSEMBLY_{0}_READING_ERROR=Error by reading assembly '{0}' diff --git a/bin/Lng/Rus/SemanticErrors_ib.dat b/bin/Lng/Rus/SemanticErrors_ib.dat index 4722b6f64..e8a931ed4 100644 --- a/bin/Lng/Rus/SemanticErrors_ib.dat +++ b/bin/Lng/Rus/SemanticErrors_ib.dat @@ -113,6 +113,7 @@ ANONYMOUS_FUNCTION_TYPE_WITH_GENERICS=Недопустимо описание а CLASS_NOT_IMPLEMENT_THIS_INTERFACE=Класс не реализует данный интерфейс INTERFACE_EXPECTED=Ожидался интерфейс IMPLICIT_EXPLICIT_OPERATOR_EXTENSION_ONLY_FOR_COMPILED_CLASSES_ALLOWED=extension-операторы преобразования типов допустимы только для классов из других сборок .NET +EXTENSIONMETHOD_KEYWORD_NOT_ALLOWED=Атрибут extensionmethod недопустим в данном контексте %PREFIX%=COMPILATIONERROR_ UNIT_MODULE_EXPECTED_LIBRARY_FOUND=Ожидался модуль, а встречена библиотека ASSEMBLY_{0}_READING_ERROR=Ошибка при чтении сборки '{0}' diff --git a/bin/PT4/Lib/PT4ExamBegin_ru.dll b/bin/PT4/Lib/PT4ExamBegin_ru.dll index b3e5ef917..8015453af 100644 Binary files a/bin/PT4/Lib/PT4ExamBegin_ru.dll and b/bin/PT4/Lib/PT4ExamBegin_ru.dll differ diff --git a/bin/PT4/Lib/PT4ExamCheck_ru.dll b/bin/PT4/Lib/PT4ExamCheck_ru.dll index ba42e2c15..25634eb42 100644 Binary files a/bin/PT4/Lib/PT4ExamCheck_ru.dll and b/bin/PT4/Lib/PT4ExamCheck_ru.dll differ diff --git a/bin/PT4/Lib/PT4ExamTaskC_ru.dll b/bin/PT4/Lib/PT4ExamTaskC_ru.dll index 43c826627..825a50314 100644 Binary files a/bin/PT4/Lib/PT4ExamTaskC_ru.dll and b/bin/PT4/Lib/PT4ExamTaskC_ru.dll differ