|
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using TinyPG;
using TinyPG.Compiler;
namespace TinyPG.CodeGenerators.CSharp
{
public class ScannerGenerator : ICodeGenerator
{
internal ScannerGenerator()
{
}
public string FileName
{
get { return "Scanner.cs"; }
}
public string Generate(Grammar Grammar, bool Debug)
{
if (string.IsNullOrEmpty(Grammar.GetTemplatePath()))
return null;
string scanner = File.ReadAllText(Grammar.GetTemplatePath() + FileName);
int counter = 2;
StringBuilder tokentype = new StringBuilder();
StringBuilder regexps = new StringBuilder();
StringBuilder skiplist = new StringBuilder();
foreach (TerminalSymbol s in Grammar.SkipSymbols)
{
skiplist.AppendLine(" SkipList.Add(TokenType." + s.Name + ");");
}
// build system tokens
tokentype.AppendLine("\r\n //Non terminal tokens:");
tokentype.AppendLine(Helper.Outline("_NONE_", 3, "= 0,", 5));
tokentype.AppendLine(Helper.Outline("_UNDETERMINED_", 3, "= 1,", 5));
// build non terminal tokens
tokentype.AppendLine("\r\n //Non terminal tokens:");
foreach (Symbol s in Grammar.GetNonTerminals())
{
tokentype.AppendLine(Helper.Outline(s.Name, 3, "= " + String.Format("{0:d},", counter), 5));
counter++;
}
// build terminal tokens
tokentype.AppendLine("\r\n //Terminal tokens:");
bool first = true;
foreach (TerminalSymbol s in Grammar.GetTerminals())
{
regexps.Append(" regex = new Regex(" + s.Expression.ToString() + ", RegexOptions.Compiled);\r\n");
regexps.Append(" Patterns.Add(TokenType." + s.Name + ", regex);\r\n");
regexps.Append(" Tokens.Add(TokenType." + s.Name + ");\r\n\r\n");
if (first) first = false;
else tokentype.AppendLine(",");
tokentype.Append(Helper.Outline(s.Name, 3, "= " + String.Format("{0:d}", counter), 5));
counter++;
}
scanner = scanner.Replace(@"<%SkipList%>", skiplist.ToString());
scanner = scanner.Replace(@"<%RegExps%>", regexps.ToString());
scanner = scanner.Replace(@"<%TokenType%>", tokentype.ToString());
if (Debug)
{
scanner = scanner.Replace(@"<%Namespace%>", "TinyPG.Debug");
scanner = scanner.Replace(@"<%IToken%>", " : TinyPG.Debug.IToken");
}
else
{
scanner = scanner.Replace(@"<%Namespace%>", Grammar.Directives["TinyPG"]["Namespace"]);
scanner = scanner.Replace(@"<%IToken%>", "");
}
return scanner;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Currently Herre Kuijpers is employed at Rubicon. During his career he developed skills with all kinds of technologies, methodologies and programming languages such as c#, ASP.Net, .Net Core, VC++, Javascript, SQL, Agile, Scrum, DevOps, ALM. Currently he fulfills the role of software architect in various projects.
Herre Kuijpers is a very experienced software architect with deep knowledge of software design and development on the Microsoft .Net platform. He has a broad knowledge of Microsoft products and knows how these, in combination with custom software, can be optimally implemented in the often complex environment of the customer.