/// <exclude/> public int EmitClassDefin(string b,ref int p,int max,CsReader inf,string defbas,out string bas, out string name,bool lx) { name = ""; bas = defbas; NonWhite(b,ref p,max); White(b,ref p,max); for(;p<max&&b[p]!=':'&&b[p]!=';';p++) name += b[p]; if (b[p]==':') for(p++,bas="";p<max&&b[p]!=';';p++) bas += b[p]; if (b[p]!=';') Error(48,p,"Bad script"); int num = new TokClassDef(this,name,bas).m_yynum; m_outFile.WriteLine("//%+{0}+{1}",name,num); m_outFile.Write("public class "); m_outFile.Write(name); m_outFile.Write(" : "+bas); m_outFile.WriteLine("{"); m_outFile.WriteLine("public override string yyname { get { return \""+name+"\"; }}"); m_outFile.WriteLine("public override int yynum { get { return "+num+"; }}"); if (lx) m_outFile.WriteLine("public "+name+"(Lexer yym):base(yym){ }}"); else m_outFile.WriteLine("public "+name+"(Parser yyq):base(yyq){ }}"); return num; }
public static object Serialise(object o, Serialiser s) { if (s == null) { return(new TokClassDef()); } TokClassDef t = (TokClassDef)o; if (s.Encode) { s.Serialise(t.m_name); return(null); } t.m_name = (string)s.Deserialise(); return(t); }
public static ResWds New(TokensGen tks, string str) { ResWds r = new ResWds(); str = str.Trim(); if (str[0] == 'U') { r.m_upper = true; str = str.Substring(1).Trim(); } if (str[0] != '{' || str[str.Length - 1] != '}') { goto bad; } str = str.Substring(1, str.Length - 2).Trim(); string[] wds = str.Split(','); for (int j = 0; j < wds.Length; j++) { string w = wds[j].Trim(); string a = w; int i = w.IndexOf(' '); if (i > 0) { a = w.Substring(i).Trim(); w = w.Substring(0, i); } r.m_wds[w] = a; if (tks.m_tokens.tokens[a] == null) { TokClassDef t = new TokClassDef(tks, a, "TOKEN"); tks.m_outFile.WriteLine("//%{0}+{1}", a, t.m_yynum); tks.m_outFile.Write("public class {0} : TOKEN", a); tks.m_outFile.WriteLine("{ public override string yyname { get { return \"" + a + "\";}}"); tks.m_outFile.WriteLine("public override int yynum { get { return " + t.m_yynum + "; }}"); tks.m_outFile.WriteLine(" public " + a + "(Lexer yyl):base(yyl) {}}"); } } return(r); bad: tks.m_tokens.erh.Error(new CSToolsException(47, "bad ResWds element")); return(null); }
public static ResWds New(TokensGen tks, string str) { ResWds resWds = new ResWds(); str = str.Trim(); if (str[0] == 'U') { resWds.m_upper = true; str = str.Substring(1).Trim(); } if (str[0] == '{' && str[str.Length - 1] == '}') { str = str.Substring(1, str.Length - 2).Trim(); string str1 = str; char[] chArray = new char[1] { ',' }; foreach (string str2 in str1.Split(chArray)) { string str3 = str2.Trim(); string name = str3; int num = str3.IndexOf(' '); if (num > 0) { name = str3.Substring(num).Trim(); str3 = str3.Substring(0, num); } resWds.m_wds[(object)str3] = (object)name; if (tks.m_tokens.tokens[(object)name] == null) { TokClassDef tokClassDef = new TokClassDef((GenBase)tks, name, "TOKEN"); tks.m_outFile.WriteLine("//%{0}+{1}", (object)name, (object)tokClassDef.m_yynum); tks.m_outFile.Write("public class {0} : TOKEN", (object)name); tks.m_outFile.WriteLine("{ public override string yyname { get { return \"" + name + "\";}}"); tks.m_outFile.WriteLine("public override int yynum { get { return " + (object)tokClassDef.m_yynum + "; }}"); tks.m_outFile.WriteLine(" public " + name + "(Lexer yyl):base(yyl) {}}"); } } return(resWds); } tks.m_tokens.erh.Error(new CSToolsException(47, "bad ResWds element")); return((ResWds)null); }
public int EmitClassDefin(string b, ref int p, int max, CsReader inf, string defbas, out string bas, out string name, bool lx) { name = ""; bas = defbas; NonWhite(b, ref p, max); White(b, ref p, max); for (; p < max && b[p] != ':' && b[p] != ';'; p++) { name += b[p]; } if (b[p] == ':') { for (p++, bas = ""; p < max && b[p] != ';'; p++) { bas += b[p]; } } if (b[p] != ';') { Error(48, p, "Bad script"); } int num = new TokClassDef(this, name, bas).m_yynum; m_outFile.WriteLine("//%+{0}+{1}", name, num); m_outFile.Write("public class "); m_outFile.Write(name); m_outFile.Write(" : " + bas); m_outFile.WriteLine("{"); m_outFile.WriteLine("public override string yyname { get { return \"" + name + "\"; }}"); m_outFile.WriteLine("public override int yynum { get { return " + num + "; }}"); if (lx) { m_outFile.WriteLine("public " + name + "(Lexer yym):base(yym){ }}"); } else { m_outFile.WriteLine("public " + name + "(Parser yyq):base(yyq){ }}"); } return(num); }
public int EmitClassDefin( string b, ref int p, int max, CsReader inf, string defbas, out string bas, out string name, bool lx) { bool flag = false; name = ""; bas = defbas; if (lx) { this.NonWhite(b, ref p, max); } this.White(b, ref p, max); while (p < max && b[p] != '{' && (b[p] != ':' && b[p] != ';') && (b[p] != ' ' && b[p] != '\t' && b[p] != '\n')) { name += (string)(object)b[p]; ++p; } this.White(b, ref p, max); if (b[p] == ':') { ++p; this.White(b, ref p, max); bas = ""; while (p < max && b[p] != ' ' && (b[p] != '{' && b[p] != '\t') && (b[p] != ';' && b[p] != '\n')) { bas += (string)(object)b[p]; ++p; } } int yynum = new TokClassDef(this, name, bas).m_yynum; this.m_outFile.WriteLine("//%+{0}+{1}", (object)name, (object)yynum); this.m_outFile.Write("public class "); this.m_outFile.Write(name); this.m_outFile.Write(" : " + bas); this.m_outFile.WriteLine("{"); do { if (p >= max) { b += inf.ReadLine(); max = b.Length; } this.White(b, ref p, max); }while (p >= max); if (b[p] != ';') { cs0syntax cs0syntax = new cs0syntax((YyParser) new yycs0syntax(), this.erh); ((cs0tokens)cs0syntax.m_lexer).Out = this.m_outname; cs0syntax.Cls = name; cs0syntax.Out = this.m_outname; if (lx) { cs0syntax.Ctx = "Lexer yyl"; cs0syntax.Par = "yym"; } else { cs0syntax.Ctx = "Parser yyp"; cs0syntax.Par = "yyq"; } string braceIfFound = this.ToBraceIfFound(ref b, ref p, ref max, inf); TOKEN token = (TOKEN)null; try { token = (TOKEN)cs0syntax.Parse(braceIfFound); } catch (Exception) { } if (token == null) { this.Error(48, p, "Bad class definition for " + name); return(-1); } token.yytext = token.yytext.Replace("yyq", "((" + this.m_outname + ")yyp)"); token.yytext = token.yytext.Replace("yym", "((" + this.m_outname + ")yyl)"); string yytext = token.yytext; char[] chArray = new char[1] { '\n' }; foreach (string str in yytext.Split(chArray)) { this.m_outFile.WriteLine(str); } flag = cs0syntax.defconseen; } this.m_outFile.WriteLine("public override string yyname { get { return \"" + name + "\"; }}"); this.m_outFile.WriteLine("public override int yynum { get { return " + (object)yynum + "; }}"); if (!flag) { if (lx) { this.m_outFile.Write("public " + name + "(Lexer yyl):base(yyl){}"); } else { this.m_outFile.Write("public " + name + "(Parser yyp):base(yyp){}"); } } this.m_outFile.WriteLine("}"); return(yynum); }
/// <exclude/> public static ResWds New(TokensGen tks,string str) { ResWds r = new ResWds(); str = str.Trim(); if (str[0]=='U') { r.m_upper = true; str = str.Substring(1).Trim(); } if (str[0]!='{' || str[str.Length-1]!='}') goto bad; str = str.Substring(1,str.Length-2).Trim(); string[] wds = str.Split(','); for (int j=0;j<wds.Length;j++) { string w = wds[j].Trim(); string a = w; int i = w.IndexOf(' '); if (i>0) { a = w.Substring(i).Trim(); w = w.Substring(0,i); } r.m_wds[w] = a; if (tks.m_tokens.tokens[a]==null) { TokClassDef t = new TokClassDef(tks,a,"TOKEN"); tks.m_outFile.WriteLine("//%{0}+{1}",a,t.m_yynum); tks.m_outFile.Write("public class {0} : TOKEN",a); tks.m_outFile.WriteLine("{ public override string yyname { get { return \""+a+"\";}}"); tks.m_outFile.WriteLine("public override int yynum { get { return "+t.m_yynum+"; }}"); tks.m_outFile.WriteLine(" public "+a+"(Lexer yyl):base(yyl) {}}"); } } return r; bad: tks.m_tokens.erh.Error(new CSToolsException(47,"bad ResWds element")); return null; }
public int EmitClassDefin(string b,ref int p,int max,CsReader inf,string defbas,out string bas, out string name,bool lx) { bool defconseen = false; name = ""; bas = defbas; if (lx) NonWhite(b,ref p,max); White(b,ref p,max); for(;p<max&&b[p]!='{'&&b[p]!=':'&&b[p]!=';'&&b[p]!=' '&&b[p]!='\t'&&b[p]!='\n';p++) name += b[p]; White(b,ref p,max); if (b[p]==':') { p++; White(b,ref p,max); for(bas="";p<max&&b[p]!=' '&&b[p]!='{'&&b[p]!='\t'&&b[p]!=';'&&b[p]!='\n';p++) bas += b[p]; } int num = new TokClassDef(this,name,bas).m_yynum; m_outFile.WriteLine("//%+{0}+{1}",name,num); m_outFile.Write("public class "); m_outFile.Write(name); m_outFile.Write(" : "+bas); m_outFile.WriteLine("{"); do { if (p>=max) { b += inf.ReadLine(); max = b.Length; } White(b,ref p,max); } while (p>=max); if (b[p]!=';') { cs0syntax syms = new cs0syntax(new yycs0syntax(),erh); cs0tokens tks = (cs0tokens)syms.m_lexer; tks.Out = m_outname; // syms.m_debug = true; syms.Cls = name; syms.Out = m_outname; if (lx) { syms.Ctx = "Lexer yyl"; syms.Par = "yym"; } else { syms.Ctx = "Parser yyp"; syms.Par = "yyq"; } string str = ToBraceIfFound(ref b,ref p, ref max,inf); TOKEN s = null; try // 4.7c { s = (TOKEN)syms.Parse(str); } catch (Exception) {} if (s==null) { Error(48,p,"Bad class definition for "+name); return -1; } s.yytext = s.yytext.Replace("yyq","(("+m_outname+")yyp)"); s.yytext = s.yytext.Replace("yym","(("+m_outname+")yyl)"); string[] ss = s.yytext.Split('\n'); for (int j=0;j<ss.Length;j++) m_outFile.WriteLine(ss[j]); defconseen = syms.defconseen; } m_outFile.WriteLine("public override string yyname { get { return \""+name+"\"; }}"); m_outFile.WriteLine("public override int yynum { get { return "+num+"; }}"); if (!defconseen) { if (lx) m_outFile.Write("public "+name+"(Lexer yyl):base(yyl){}"); else m_outFile.Write("public "+name+"(Parser yyp):base(yyp){}"); } m_outFile.WriteLine("}"); return num; }
public int EmitClassDefin(string b, ref int p, int max, CsReader inf, string defbas, out string bas, out string name, bool lx) { bool defconseen = false; name = ""; bas = defbas; if (lx) { NonWhite(b, ref p, max); } White(b, ref p, max); for (; p < max && b[p] != '{' && b[p] != ':' && b[p] != ';' && b[p] != ' ' && b[p] != '\t' && b[p] != '\n'; p++) { name += b[p]; } White(b, ref p, max); if (b[p] == ':') { p++; White(b, ref p, max); for (bas = ""; p < max && b[p] != ' ' && b[p] != '{' && b[p] != '\t' && b[p] != ';' && b[p] != '\n'; p++) { bas += b[p]; } } int num = new TokClassDef(this, name, bas).m_yynum; m_outFile.WriteLine("//%+{0}+{1}", name, num); m_outFile.Write("public class "); m_outFile.Write(name); m_outFile.Write(" : " + bas); m_outFile.WriteLine("{"); do { if (p >= max) { b += inf.ReadLine(); max = b.Length; } White(b, ref p, max); } while (p >= max); if (b[p] != ';') { cs0syntax syms = new cs0syntax(new yycs0syntax(), erh); cs0tokens tks = (cs0tokens)syms.m_lexer; tks.Out = m_outname; // syms.m_debug = true; syms.Cls = name; syms.Out = m_outname; if (lx) { syms.Ctx = "Lexer yyl"; syms.Par = "yym"; } else { syms.Ctx = "Parser yyp"; syms.Par = "yyq"; } string str = ToBraceIfFound(ref b, ref p, ref max, inf); TOKEN s = null; try // 4.7c { s = (TOKEN)syms.Parse(str); } catch (Exception) {} if (s == null) { Error(48, p, "Bad class definition for " + name); return(-1); } s.yytext = s.yytext.Replace("yyq", "((" + m_outname + ")yyp)"); s.yytext = s.yytext.Replace("yym", "((" + m_outname + ")yyl)"); string[] ss = s.yytext.Split('\n'); for (int j = 0; j < ss.Length; j++) { m_outFile.WriteLine(ss[j]); } defconseen = syms.defconseen; } m_outFile.WriteLine("public override string yyname { get { return \"" + name + "\"; }}"); m_outFile.WriteLine("public override int yynum { get { return " + num + "; }}"); if (!defconseen) { if (lx) { m_outFile.Write("public " + name + "(Lexer yyl):base(yyl){}"); } else { m_outFile.Write("public " + name + "(Parser yyp):base(yyp){}"); } } m_outFile.WriteLine("}"); return(num); }
void _Create() { m_outFile.WriteLine("using System;using Tools;"); m_tokens = new YyLexer(erh); string buf = ""; string str = ""; string name=""; string startsym; Nfa nfa; int p,q,max; Console.WriteLine("Reading Input File"); while (!m_inFile.Eof()) { buf = m_inFile.ReadLine(); startsym = "YYINITIAL"; max = buf.Length; p = 0; if (!White(buf,ref p,max)) continue; if (buf[p]=='%') { // directive // %lexer if(buf.Length>=p+6 && "%lexer".Equals(buf.Substring(p,6))) { m_lexerseen = true; p+=6; if (!White(buf,ref p, max)) continue; q = p; NonWhite(buf,ref p, max); if (q!=p) m_outname = buf.Substring(q,p-q); continue; } // %encoding if (buf.Length>=p+9 && "%encoding".Equals(buf.Substring(p,9))) { p+=9; White(buf,ref p, max); q = p; NonWhite(buf,ref p, max); m_tokens.InputEncoding = buf.Substring(q,p-q); continue; } // %namespace if (buf.Length>=p+10 && "%namespace".Equals(buf.Substring(p,10))) { p+=10; White(buf,ref p,max); q = p; NonWhite(buf,ref p,max); m_outFile.WriteLine("namespace "+buf.Substring(q,p-q)+" {"); m_namespace = true; continue; } // %define if(buf.Length>=p+7 && "%define".Equals(buf.Substring(p,7))) { p+=7; White(buf,ref p,max); q = p; if (!NonWhite(buf,ref p,max)) { erh.Error(new CSToolsException(44,"Bad define")); continue; } name=buf.Substring(q,p-q); p++; if (White(buf,ref p,max)) defines[name]=buf.Substring(p,max-p); } else // % token/node if (buf.Length>=p+6 && "%token".Equals(buf.Substring(p,6))) EmitClassDefin(buf,ref p,max,m_inFile,"TOKEN", out str,out name,true); else if (buf.Length>=p+5 && "%node".Equals(buf.Substring(p,5))) EmitClassDefin(buf,ref p,max,m_inFile,"NODE",out str,out name,true); else if (buf.Length>=p+2 && "%{".Equals(buf.Substring(p,2))) CopyCode(); else if (buf.Length>=p+9 && "%declare{".Equals(buf.Substring(p,9))) { p += 8; m_actvars = ToBraceIfFound(ref buf,ref p,ref max,m_inFile); m_actvars = m_actvars.Substring(1,m_actvars.Length-2); } else m_tokens.erh.Error(new CSToolsException(8,"Unknown directive "+buf.Substring(p,max-p))); continue; } else if (buf[p]=='<') { // startstate q = p++; while (p<max && buf[p]!='>') p++; if (p++ ==max) { m_tokens.erh.Error(new CSToolsException(25,"Bad startsymbol")); continue; } startsym = buf.Substring(q+1,p-q-2); White(buf, ref p, max); } q=p; // can't simply look for nonwhite space here because embedded spaces GetRegex(buf,ref p,max); string trgx = buf.Substring(q,p-q); if (m_tokens.toupper) trgx = trgx.ToUpper(); Regex rgx = new Regex(this,q,trgx); Nfa nfa1= new Nfa(this,rgx); if (!m_startstates.Contains(startsym)) m_startstates[startsym] = new Nfa(this); nfa = (Nfa)m_startstates[startsym]; nfa.AddEps(nfa1); White(buf,ref p,max); m_actions[nfa1.m_end.m_state] = nfa1.m_end; // handle multiline actions enclosed in {} nfa1.m_end.m_sTerminal = ToBraceIfFound(ref buf,ref p, ref max,m_inFile); // examine action string if (nfa1.m_end.m_sTerminal.Length>0 && nfa1.m_end.m_sTerminal[0] == '%') { string tokClass,b = nfa1.m_end.m_sTerminal; q = 1; max = b.Length; int n; for (n=0;q<max&&b[q]!=' '&&b[q]!='\t'&&b[q]!='\n'&&b[q]!='{'&&b[q]!=':';q++,n++) // extract the class name ; tokClass = b.Substring(1,n); // new-style auto token construction object ob = m_tokens.tokens[tokClass]; TokClassDef t = (TokClassDef)ob; bool isNew = (t==null); // check for initialisation action following %name string init = b.Substring(n+1,b.Length-n-1); string bas1 = "TOKEN"; bool haveInit = false; for (int j=0;j<init.Length;j++) if (init[j]=='{') { haveInit = true; break; } else if (init[j]==':') { bas1 = ""; for (;init[j]==' '||init[j]=='\r';j++) ; for (;init[j]!=' '&&init[j]!='\t'&&init[j]!='{'&&init[j]!='\n';j++) bas1 += init[j]; break; } if (isNew && tokClass!="TOKEN") { // this token class has not been declared. Do so now bool isNode = (m_tokens.tokens[bas1]!=null); t = new TokClassDef(this,tokClass,bas1); // updates TOKEN.tokens m_outFile.WriteLine("//%{0}+{1}",tokClass,t.m_yynum); m_outFile.Write("public class {0} : {1}",tokClass,bas1); m_outFile.WriteLine("{ public override string yyname { get { return \""+tokClass+"\";}}"); m_outFile.WriteLine("public override int yynum { get { return "+t.m_yynum+"; }}"); m_outFile.WriteLine(" public "+tokClass+"(Lexer yyl):base(yyl) {}}"); } if (haveInit && init.IndexOf("%except")<0) { init = init.Replace("yylval","yymval"); // yuk: avoid the next line munging yylval init = FixActions(init); init = init.Replace("yymval","yylval"); nfa1.m_end.m_sTerminal = "%"+NewConstructor(t,init); } } } if (!m_lexerseen) m_tokens.erh.Error(new CSToolsException(26,"No %lexer directive detected: possibly incorrect text encoding?")); Console.WriteLine("Constructing DFAs"); foreach (string s in m_startstates.Keys) { Dfa d = new Dfa((Nfa)m_startstates[s]); m_tokens.starts[s] = d; if (d.m_actions!=null) Console.WriteLine("Warning: This lexer script generates an infinite token stream on bad input"); } Console.WriteLine("Output phase"); Emit(m_actions,m_actvars,m_namespace,m_showDfa); Console.WriteLine("End of Create"); object o = m_tokens.starts["YYINITIAL"]; if (o == null) Console.WriteLine("Warning: No lexer"); else if (((Dfa)o).m_actions!=null) // repeat the above warning Console.WriteLine("Warning: This lexer script generates an infinite token stream on bad input"); }
string NewConstructor(TokClassDef pT, string str) { if (str=="") return ""; string bas = "TOKEN"; if (pT!=null) bas = pT.m_name; string newname = ""; char[] buf = new char[32]; for (int variant=1;;variant++) { // ensure we get a unique name newname = String.Format("{0}_{1}",bas,variant); object o = m_tokens.tokens[newname]; if (o==null) break; } pT = new TokClassDef(this,newname,bas); m_outFile.WriteLine("public class "+newname+" : "+bas+" {"); m_outFile.Write(" public "+newname+"(Lexer yym):base(yym) "); pT.m_initialisation = FixActions(str); m_outFile.WriteLine(str + "}"); return newname; }