public static Token GetCmdLnArgs(string[] args) { if (args == null) throw new ArgumentNullException("args"); List<Token> opts = new List<Token>(); List<Token> srcs = new List<Token>(); Action<string> Pick = delegate(string arg) { if (arg == null) return; arg = arg.Trim(); if (arg == "") return; Token t; if (Regex.IsMatch(arg, OptHead)) { if ((t = PickOpt(arg)) == null) { throw new Exception(string.Format("Not supported option: {0}", arg)); } opts.Add(t); } else { srcs.Add(PickSrc(arg)); } }; foreach (string arg in args) { Pick(arg); } Token srct = new Token("", "Sources"); srct.Follows = srcs.ToArray(); Token optt = new Token("", "CompileOptions"); optt.Follows = opts.ToArray(); if (0 == optt.Select("out").Length && srcs.Count > 0) { string v = srcs[0].Value; string dir = System.IO.Path.GetDirectoryName(v); string fn = System.IO.Path.GetFileNameWithoutExtension(v) + ".exe"; optt.FlwsAdd(NewOpt("out", System.IO.Path.Combine(dir, fn))); } Token ret = new Token("", "Arguments"); ret.Follows = new Token[] { optt, srct }; return ret; }
public static void AnalyzeSyntax(Token root) { SyntaxAnalyzer analyzer = new SyntaxAnalyzer(); Token srcs = root.Find("Sources"); Token syntax = root.Find("Syntax"); List<Token> synflw = new List<Token>(); foreach (Token f in srcs.Follows) { if (f.Group == "SourcePath") { continue; } if (f.Group == "SourceText") { synflw.Add(analyzer.Run(f.Value, f.First.Value)); } } syntax.Follows = synflw.ToArray(); }
public static void Check(Token root) { if (root == null) { throw new ArgumentNullException("args"); } if (0 == root.Select("CompileOptions").Length) { throw new ArgumentException("No @CompileOptions Token"); } if (1 < root.Select("CompileOptions").Length) { throw new ArgumentException("Too many @CompileOptions Token"); } if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("No source filename is specified to command line parameter"); } if (1 < root.Select("Sources").Length) { throw new ArgumentException("Too many @Sources Token"); } if (0 == root.Select("CompileOptions/@out").Length) { if (0 == root.Select("Sources").Length || 0 == root.Find("Sources").Follows.Length) { throw new ArgumentException("Cannot omit source path when out option was omitted"); } } if (1 < root.Select("CompileOptions/out").Length) { throw new ArgumentException("Too many out options are specified to command line parameter"); } if (1 == root.Select("Sources").Length) { foreach (Token p in root.Select("Sources/SourcePath")) { if (false == File.Exists(p.Value)) { if (false == File.Exists(p.Value + ".nana")) { throw new FileNotFoundException("Source file was not found", p.Value); } else { p.Value += ".nana"; } } } } }
public static Token CreateParamToken(string[] nameAndTypes) { Token cur = null; Token cma = null; foreach (string nt in nameAndTypes) { if (null != cma) { Token tmp = cma; cma = new Token(",", "_End_Cma_"); cma.First = tmp; } string[] spl = nt.Split(new char[] { ':' }); cur = new Token(":", "TypSpc"); cur.First = new Token(spl[0], "Id"); cur.Second = new Token(spl[1], "Id"); if (cma == null) { cma = cur; } else { cma.Second = cur; } } return cma; }
public EnvAnalyzer(Token seed) : base(seed, /*above*/ BlkAnalyzer.EmptyBlz) { Ez = this; }
public AccessError(string message, Token t) : base(message, t.Path, t.Row, t.Col) { }
public IMRTranslation(string message, Token t) : base(message, t.Path, t.Row, t.Col) { }
public void Next() { _Cur = NoMore; Token cur = null; do { if (R.BOF) { CallReadLine(); if (R.EOF) return; } SkipSpace(); while (EOL) { CallReadLine(); if (R.EOF) return; SkipSpace(); } cur = GetToken(); } while (SkipGroups.Contains(cur.Group)); _Cur = cur; }
public SemanticError(string message, Token t) : base(message, t) { }
public override Token GetToken() { string ln = SubCurLine; if (IsTokenStart() == false) throw new Exception(); Match startMt = StartRx.Match(ln); Token t = new Token(); t.Group = Group; StringBuilder b = new StringBuilder(); Match escMt = null, endMt = null; int startIdx = startMt.Length; Action match = delegate() { escMt = EscRx.Match(ln, startIdx); endMt = EndRx.Match(ln, startIdx); if (endMt.Success) { if (escMt.Success && escMt.Index < endMt.Index) { endMt = null; startIdx = escMt.Index + escMt.Length; } } else { endMt = null; startIdx = ln.Length; } }; match(); while (ln != null && endMt == null) { if (startIdx >= ln.Length) { b.AppendLine(ln); CallReadLine(); if (R.EOF) break; ln = SubCurLine; startIdx = 0; } match(); } if (endMt == null) throw new Exception(@"No end"); b.Append(ln.Substring(0, endMt.Index + endMt.Length)); t.Value = b.ToString(); Pos.Value += endMt.Index + endMt.Length; return t; }
public Prepend(ITokenEnumerator enm, Token value) { IsFirst = true; Enm = enm; Value = value; }
public static Token CreateClassToken(string name, string basename) { Token root = new Token("class", "Typ"); root.FlwsAdd(name, "Name"); if (null != basename) { Token bs = new Token("->", "BaseTypeDef"); bs.FlwsAdd(basename, "Id"); root.FlwsAdd(bs); } root.FlwsAdd("...", "Block") .FlwsAdd(",,,"); return root; }
public static Token CreateFunToken(string func, string name, string returnType) { Debug.Assert(false == string.IsNullOrEmpty(func)); Token f; f = new Token(func, "Fun"); if (string.IsNullOrEmpty(name) == false) { f.FlwsAdd(name, "Name"); } f.FlwsAdd("(", "Prm").FlwsAdd(")"); if (string.IsNullOrEmpty(returnType) == false) { f.FlwsAdd(":", "TypSpc"); f.FlwsTail.FlwsAdd(returnType, "Id"); } f.FlwsAdd("..", "Block"); f.FlwsTail.Follows = new Token[0]; return f; }
public LineAnalyzer(Token seed, BlkAnalyzer above) { Seed = seed; Above = above; Breaks = new Stack<Literal>(); Continues = new Stack<Literal>(); if (null != above && null != above.Ez) { E = Above.Ez.E; } }
public BlkAnalyzer NewBlz(Token t) { BlkAnalyzer blz = new BlkAnalyzer(t, this); Blzs.AddLast(blz); return blz; }
public static string AnalyzeName(string ftyp, Token seed) { string nameasm = null; if (ftyp == "cons") { nameasm = Nana.IMRs.IMRGenerator.InstCons; } if (ftyp == "scons") { nameasm = Nana.IMRs.IMRGenerator.StatCons; } if (ftyp == "sfun" || ftyp == "nfun" || ftyp == "vfun") { nameasm = seed.Follows[0].Value; } return nameasm; }
public FunAnalyzer(Token seed, BlkAnalyzer above) : base(seed, above) { CopyAboveAnalyzers(above); Blz = Fuz = this; }
public static Env Run(Token root) { EnvAnalyzer ez = new EnvAnalyzer(root); ez.AnalyzeEnv(); return ez.E; }
public override Token GetToken() { string ln = SubCurLine; if (IsTokenStart() == false) throw new Exception(); string[] groups; string g = ""; Match m = StartRx.Match(ln); Token t = new Token(); groups = StartRx.GetGroupNames(); for (int i = 1; i < groups.Length; i++) { if (m.Groups[i].Success) { g = groups[i]; break; } } t = new Token(); t.Value = m.Value; t.Group = g; t.Path = R.Path; t.Row = R.ReadCount; t.Col = Pos.Value; Pos.Value += m.Index + m.Length; return t; }
public static Token CreateVarToken(Variable v) { Token t = new Token(":", "TypSpc"); t.First = new Token(v.Name, "Id"); t.Second = new Token(v.Att.TypGet.Name, "Id"); return t; }
public TokenizerBase(Regex startRx, List<string> skipGroups) { _Cur = null; StartRx = startRx; SkipGroups = skipGroups; }
public static void Prepare(Token root) { if (false == root.Contains("Syntax")) { root.FlwsAdd(new Token("", "Syntax")); } if (false == root.Contains("Code")) { root.FlwsAdd(new Token("", "Code")); } }
public Append(ITokenEnumerator enm, Token value) { IsInnerEOF = false; Enm = enm; Value = value; }
public static void ReadSourceFiles(Token root) { Token srcs = root.Find("Sources"); UTF8Encoding utf8 = new UTF8Encoding(false /* no byte order mark */); List<Token> srcsflw = new List<Token>(); foreach (Token f in srcs.Follows) { srcsflw.Add(f); if (f.Group == "SourceText") { f.First = new Token(""); continue; } if (f.Group == "SourcePath") { string text = File.ReadAllText(f.Value, utf8); Token txtt = new Token(text, "SourceText"); txtt.First = f; srcsflw.Add(txtt); } } srcs.Follows = srcsflw.ToArray(); }
public SyntaxError(string message, Token t) : base(message, t.Path, t.Row, t.Col) { }
public void Compile(Token root) { Prepare(root); Token srcs = root.Find("Sources"); // append SourceText if it's SourcePath ReadSourceFiles(root); AnalyzeSyntax(root); AfterSyntaxAnalyze(root); Env env = AnalyzeSemantic(root); AfterSemanticAnalyze(root, env); IMRGenerator imrgen = new IMRGenerator(); imrgen.GenerateIMR(env.Ap); Token code = root.Find("Code"); CodeGenerator codegen = new CodeGenerator(); code.Value = codegen.GenerateCode(env); }
public Error(string message, Token t) : this(message, t.Path, t.Row, t.Col) { }
public static Env AnalyzeSemantic(Token root) { return EnvAnalyzer.Run(root); }
public InternalError(string message, Token t) : base(message, t.Path, t.Row, t.Col) { }
public CustomAnalyzer(Token seed, BlkAnalyzer above) : base(seed, above) { }