pascalabcnet/Grammars/PascalABC.NET/Preprocessor2.pgt
Бондарев Иван e6e67c193c initial commit
2015-05-14 21:35:07 +02:00

269 lines
8.3 KiB
Plaintext

##TEMPLATE-NAME '_ C# - Calitha Engine - PascalABC.NET'
##LANGUAGE 'C#'
##ENGINE-NAME 'Calitha GOLD Parser Engine'
##AUTHOR 'DarkStar'
##FILE-EXTENSION 'cs'
##NOTES
PascalABC.NET parser template.
Version 0.3
##END-NOTES
##ID-CASE UPPERCASE
##ID-SEPARATOR '_'
##ID-SYMBOL-PREFIX 'Symbol'
##ID-RULE-PREFIX 'Rule'
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//Äàííûé ôàéë çàïðåùåíî ïðàâèòü, îí ñîçäàåòñÿ àâòîìàòè÷åñêè!!!
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Collections;
using PascalABCCompiler.SyntaxTree;
using PascalABCCompiler.Errors;
using PascalABCCompiler.PascalABCParser.Errors;
using PascalABCCompiler.Preprocessor_2.Errors;
using PascalABCCompiler.ParserTools;
using GoldParser;
using PascalABCCompiler.PascalABCParser;
namespace PascalABCCompiler.Preprocessor2
{
public class GPB_PABCPreprocessor2 : GPBParser
{
public PascalABCCompiler.Preprocessor2.Preprocessor2 prepr;
private pascalabc_parsertools pascal_parsertools;
public GPB_PABCPreprocessor2(Stream stream, PascalABCCompiler.Preprocessor2.Preprocessor2 prepr1)
: base(stream)
{
pascal_parsertools = new pascalabc_parsertools();
parsertools = pascal_parsertools;
prepr = prepr1;
}
public GPB_PABCPreprocessor2(Stream stream, GoldParser.Grammar grammar, PascalABCCompiler.Preprocessor2.Preprocessor2 prepr1)
: base(grammar)
{
pascal_parsertools = new pascalabc_parsertools();
parsertools = pascal_parsertools;
prepr = prepr1;
}
public object Parse(string source)
{
LRParser = new Parser(new StringReader(source), LanguageGrammar);
parsertools.parser = LRParser;
LRParser.TrimReductions = true;
try
{
while (true)
{
switch (LRParser.Parse())
{
case ParseMessage.LexicalError:
errors.Add(new TokenReadError(this));
LRParser.PopInputToken();
if (errors.Count > max_errors)
return null;
break;
case ParseMessage.SyntaxError:
if ((LRParser.TokenSyntaxNode as syntax_tree_node) != null)
prev_node = LRParser.TokenSyntaxNode;
Error er = new PABCNETUnexpectedToken(this);
Symbol sym = LRParser.PopInputToken();
if (sym.SymbolType == SymbolType.End && errors.Count > 0)
return null;
errors.Add(er);
if (errors.Count > max_errors)
return null;
break;
case ParseMessage.Reduction:
LRParser.TokenSyntaxNode = CreateNonTerminalObject();
break;
case ParseMessage.Accept:
return LRParser.TokenSyntaxNode;
case ParseMessage.TokenRead:
LRParser.TokenSyntaxNode = CreateTerminalObject();
break;
case ParseMessage.InternalError:
errors.Add(new CompilerInternalError("PABCPreprocessor2", new Exception("ParseMessage.InternalError")));
return null;
case ParseMessage.NotLoadedError:
errors.Add(new CompilerInternalError("PABCPreprocessor2", new Exception("ParseMessage.NotLoadedError")));
return null;
case ParseMessage.CommentError:
errors.Add(new UnexpectedToken(this, "(EOF)"));
return null;
case ParseMessage.CommentBlockRead:
{
SourceContext sc1 = parsertools.GetTokenSourceContext(this.LRParser);
string comment = this.LRParser.CommentText;
SourceContext sc2 = new SourceContext(sc1, parsertools.GetTokenSourceContext(this.LRParser));
if (comment[0] == '{' && comment[1] == '$')
prepr.Errors.Add(new SyntaxErrorInDirective(prepr.CurrentFileName, sc2, comment));
//if (prepr.sm.AllowWrite())
//prepr.WriteToThread(sc2, comment.Replace(Environment.NewLine, " "));
//prepr.WriteToStream(sc2, comment.Replace("\r\n", " "));
}
break;
case ParseMessage.CommentLineRead:
{
SourceContext sc1 = parsertools.GetTokenSourceContext(this.LRParser);
string comment = this.LRParser.CommentText;
SourceContext sc2 = new SourceContext(sc1, sc1);
if (prepr.sm.AllowWrite())
prepr.WriteToStream(sc1, comment.Replace("\r\n", " "));
//prepr.WriteToStream(sc1, comment);
}
break;
}
}
}
catch (GoldParser.UnexpectedEOFinParseCommentBlock)
{
throw new TokenReadError(this);
}
catch (Exception e)
{
if (errors.Count > 0)
return null;
else
throw;
}
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//SymbolConstants
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
public enum SymbolConstants : int
{
##SYMBOLS
##DELIMITER ','
%ID.Padded% = %Value.Padded%%Delimiter% // %Description%
##END-SYMBOLS
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//RuleConstants
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
public enum RuleConstants : int
{
##RULES
##DELIMITER ','
%ID.Padded% = %Value.Padded%%Delimiter% // %Description%
##END-RULES
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//CreateTerminalObject
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
private Object CreateTerminalObject()
{
switch ((SymbolConstants)LRParser.TokenSymbol.Index)
{
##SYMBOLS
case SymbolConstants.%ID% :
//%Description%
//TERMINAL:%Name%
return null;
//ENDTERMINAL
##END-SYMBOLS
}
throw new SymbolException("Unknown symbol");
}
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//CreateNonTerminalObject
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
public Object CreateNonTerminalObject()
{
switch ((RuleConstants)LRParser.ReductionRule.Index)
{
##RULES
case RuleConstants.%ID% :
//%Description%
//NONTERMINAL:%Description%
return null;
//ENDNONTERMINAL
##END-RULES
}
throw new RuleException("Unknown rule");
}
}
}