public ScannerOutput(StreamWriter gen, Tab tab, bool ignoreCase, bool hasCtxMoves, State firstState, Comment firstComment) { this.gen=gen; this.tab=tab; this.ignoreCase=ignoreCase; this.hasCtxMoves=hasCtxMoves; this.firstState=firstState; this.firstComment=firstComment; keywords.Add("-->namespace",GenNamespace()); keywords.Add("-->declarations",GenDeclarations()); keywords.Add("-->initialization",GenInitialization()); keywords.Add("-->casing1",GenCasing1()); keywords.Add("-->casing2",GenCasing2()); keywords.Add("-->comments",GenComments()); keywords.Add("-->literals",GenLiterals()); keywords.Add("-->scan3", GenScan3()); keywords.Add("-->scan1",GenScan1()); keywords.Add("-->scan2",GenScan2()); keywords.Add("$$$",GenDollarDollarDollar()); }
void GenComment(Comment com, int i) { gen.WriteLine(); gen.Write ("\tbool Comment{0}() ", i); gen.WriteLine("{"); gen.WriteLine("\t\tint level = 1, pos0 = pos, line0 = line, col0 = col, charPos0 = charPos;"); if (com.start.Length == 1) { gen.WriteLine("\t\tNextCh();"); GenComBody(com); } else { gen.WriteLine("\t\tNextCh();"); gen.Write ("\t\tif ({0}) ", ChCond(com.start[1])); gen.WriteLine("{"); gen.WriteLine("\t\t\tNextCh();"); GenComBody(com); gen.WriteLine("\t\t} else {"); gen.WriteLine("\t\t\tbuffer.Pos = pos0; NextCh(); line = line0; col = col0; charPos = charPos0;"); gen.WriteLine("\t\t}"); gen.WriteLine("\t\treturn false;"); } gen.WriteLine("\t}"); }
//------------------------ scanner generation ---------------------- void GenComBody(Comment com) { gen.WriteLine( "\t\t\tfor(;;) {"); gen.Write ( "\t\t\t\tif ({0}) ", ChCond(com.stop[0])); gen.WriteLine("{"); if (com.stop.Length == 1) { gen.WriteLine("\t\t\t\t\tlevel--;"); gen.WriteLine("\t\t\t\t\tif (level == 0) { oldEols = line - line0; NextCh(); return true; }"); gen.WriteLine("\t\t\t\t\tNextCh();"); } else { gen.WriteLine("\t\t\t\t\tNextCh();"); gen.WriteLine("\t\t\t\t\tif ({0}) {{", ChCond(com.stop[1])); gen.WriteLine("\t\t\t\t\t\tlevel--;"); gen.WriteLine("\t\t\t\t\t\tif (level == 0) { oldEols = line - line0; NextCh(); return true; }"); gen.WriteLine("\t\t\t\t\t\tNextCh();"); gen.WriteLine("\t\t\t\t\t}"); } if (com.nested) { gen.Write ("\t\t\t\t}"); gen.Write(" else if ({0}) ", ChCond(com.start[0])); gen.WriteLine("{"); if (com.start.Length == 1) gen.WriteLine("\t\t\t\t\tlevel++; NextCh();"); else { gen.WriteLine("\t\t\t\t\tNextCh();"); gen.Write ("\t\t\t\t\tif ({0}) ", ChCond(com.start[1])); gen.WriteLine("{"); gen.WriteLine("\t\t\t\t\t\tlevel++; NextCh();"); gen.WriteLine("\t\t\t\t\t}"); } } gen.WriteLine( "\t\t\t\t} else if (ch == Buffer.EOF) return false;"); gen.WriteLine( "\t\t\t\telse NextCh();"); gen.WriteLine( "\t\t\t}"); }
public void NewComment(Node from, Node to, bool nested) { Comment c = new Comment(CommentStr(from), CommentStr(to), nested); c.next = firstComment; firstComment = c; }
public DFA(Parser parser) { this.parser = parser; tab = parser.tab; errors = parser.errors; trace = parser.trace; firstState = null; lastState = null; lastStateNr = -1; firstState = NewState(); firstMelted = null; firstComment = null; ignoreCase = false; dirtyDFA = false; hasCtxMoves = false; }
public void WriteScanner() { int i; string fr = tab.srcDir + "Scanner.frame"; /* pdt */ if (!File.Exists(fr)) { if (tab.frameDir != null) { fr = tab.frameDir.Trim() + Path.DirectorySeparatorChar + "Scanner.frame"; } if (!File.Exists(fr)) { throw new FatalError("Cannot find Scanner.frame"); } } try { fram = new FileStream(fr, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (FileNotFoundException) { throw new FatalError("Cannot open Scanner.frame."); } OpenGen(true); /* pdt */ if (dirtyDFA) { MakeDeterministic(); } CopyFramePart("-->begin"); if (!tab.srcName.ToLower().EndsWith("coco.atg")) { gen.Close(); OpenGen(false); /* pdt */ } CopyFramePart("-->namespace"); if (tab.nsName != null && tab.nsName.Length > 0) { gen.Write("namespace "); gen.Write(tab.nsName); gen.Write(" {"); } CopyFramePart("-->declarations"); gen.WriteLine("\tconst int maxT = {0};", tab.terminals.Count - 1); gen.WriteLine("\tconst int noSym = {0};", tab.noSym.n); if (ignoreCase) { gen.Write("\tchar valCh; // current input character (for token.val)"); } CopyFramePart("-->initialization"); WriteStartTab(); CopyFramePart("-->casing1"); if (ignoreCase) { gen.WriteLine("\t\tvalCh = (char)ch;"); gen.Write("\t\tif (ch != Buffer.EOF) ch = char.ToLower((char)ch);"); } CopyFramePart("-->casing2"); gen.Write("\t\ttval[tlen++] = "); if (ignoreCase) { gen.Write("valCh;"); } else { gen.Write("(char)ch;"); } CopyFramePart("-->comments"); Comment com = firstComment; i = 0; while (com != null) { GenComment(com, i); com = com.next; i++; } CopyFramePart("-->literals"); GenLiterals(); CopyFramePart("-->scan1"); gen.Write("\t\t\t"); if (tab.ignored.Elements() > 0) { PutRange(tab.ignored); } else { gen.Write("false"); } CopyFramePart("-->scan2"); if (firstComment != null) { gen.Write("\t\tif ("); com = firstComment; i = 0; while (com != null) { gen.Write(ChCond(com.start[0])); gen.Write(" && Comment{0}()", i); if (com.next != null) { gen.Write(" ||"); } com = com.next; i++; } gen.Write(") return NextToken();"); } if (hasCtxMoves) { gen.WriteLine(); gen.Write("\t\tint apx = 0;"); } /* pdt */ CopyFramePart("-->scan3"); for (State state = firstState.next; state != null; state = state.next) { WriteState(state); } CopyFramePart("$$$"); if (tab.nsName != null && tab.nsName.Length > 0) { gen.Write("}"); } gen.Close(); }
void GenComment(Comment com, int i) { gen.WriteLine(); gen.Write ("\tComment{0}() : bool", i); gen.WriteLine("{"); gen.WriteLine("\t\tvar level = 1; var pos0 = this.pos; var line0 = this.line; var col0 = this.col; var charPos0 = this.charPos;"); if (com.start.Length == 1) { gen.WriteLine("\t\tthis.NextCh();"); GenComBody(com); } else { gen.WriteLine("\t\tthis.NextCh();"); gen.Write ("\t\tif ({0}) ", ChCond(com.start[1])); gen.WriteLine("{"); gen.WriteLine("\t\t\tthis.NextCh();"); GenComBody(com); gen.WriteLine("\t\t} else {"); gen.WriteLine("\t\t\tthis.buffer.Pos = pos0; this.NextCh(); this.line = line0; this.col = col0; this.charPos = charPos0;"); gen.WriteLine("\t\t}"); gen.WriteLine("\t\treturn false;"); } gen.WriteLine("\t}"); }
static void GenComment(Comment com, int i) { gen.Write ("\n\tstatic bool Comment{0}() ", i); gen.WriteLine("{"); gen.WriteLine("\t\tint level = 1, line0 = line, lineStart0 = lineStart;"); if (com.start.Length == 1) { gen.WriteLine("\t\tNextCh();"); GenComBody(com); } else { gen.WriteLine("\t\tNextCh();"); gen.Write ("\t\tif ({0}) ", ChCond(com.start[1])); gen.WriteLine("{"); gen.WriteLine("\t\t\tNextCh();"); GenComBody(com); gen.WriteLine("\t\t} else {"); gen.WriteLine("\t\t\tif (ch==EOL) {line--; lineStart = lineStart0;}"); gen.WriteLine("\t\t\tpos = pos - 2; Buffer.Pos = pos+1; NextCh();"); gen.WriteLine("\t\t}"); gen.WriteLine("\t\treturn false;"); } gen.WriteLine("\t}"); }
public Comment(Node from, Node to, bool nested) { start = Str(from); stop = Str(to); this.nested = nested; this.next = first; first = this; }
void GenComment(Comment com, int i) { gen.WriteLine(); gen.WriteLine("\tmember x.Comment{0}() =", i); gen.WriteLine("\t\tlet level = ref 1 in"); gen.WriteLine("\t\tlet line0 = line in"); gen.WriteLine("\t\tlet lineStart0 = lineStart in"); gen.WriteLine("\t\tlet more = ref true in"); gen.WriteLine("\t\tlet result = ref false in"); gen.WriteLine("\t\tlet pos0 = buffer.pos in"); gen.WriteLine("\t\tlet ch0 = ch in"); if (com.start.Length == 1) { gen.WriteLine("\t\tx.NextCh();"); GenComBody(com); } else { gen.WriteLine("\t\tx.NextCh();"); gen.WriteLine("\t\tif ({0}) \n\t\tthen (", ChCond(com.start[1])); gen.WriteLine("\t\t\tx.NextCh();"); GenComBody(com); gen.WriteLine("\t\t) else ("); gen.WriteLine("\t\t\tlineStart <- lineStart0;"); gen.WriteLine("\t\t\tbuffer.pos <- pos0;"); gen.WriteLine("\t\t\tch <- ch0;"); gen.WriteLine("\t\t\tline <- line0;"); gen.WriteLine("\t\t\t false )"); } gen.WriteLine("\t"); }
//------------------------ scanner generation ---------------------- void GenComBody(Comment com) { gen.WriteLine("\t\t\t\t while !more && ch <> EOF do "); gen.WriteLine("\t\t\t\t\t if ({0}) ", ChCond(com.stop[0])); gen.WriteLine("\t\t\t\t\t then ( "); if (com.stop.Length == 1) { gen.WriteLine("\t\t\t\t\t\t level := post_dec !level;"); ; gen.WriteLine("\t\t\t\t\t\t if !level = 0 then( "); gen.WriteLine("\t\t\t\t\t\t\t oldEols <- line - line0; "); gen.WriteLine("\t\t\t\t\t\t\t x.NextCh(); "); gen.WriteLine("\t\t\t\t\t\t\t result := true; "); gen.WriteLine("\t\t\t\t\t\t\t more:= false )"); } else { gen.WriteLine("\t\t\t\t\tx.NextCh();"); gen.WriteLine("\t\t\t\t\tif ({0}) then (", ChCond(com.stop[1])); gen.WriteLine("\t\t\t\t\t\t level := post_dec !level;"); gen.WriteLine("\t\t\t\t\t\t if !level = 0 then( "); gen.WriteLine("\t\t\t\t\t\t\t oldEols <- line - line0; "); gen.WriteLine("\t\t\t\t\t\t\t x.NextCh(); "); gen.WriteLine("\t\t\t\t\t\t\t result := true; "); gen.WriteLine("\t\t\t\t\t\t\t more:= false )"); gen.WriteLine("\t\t\t\t\telse x.NextCh();"); gen.WriteLine("\t\t\t\t\t)"); } if (com.nested) { gen.WriteLine("\t\t\t\t\t ) else if ({0}) then (", ChCond(com.start[0])); if (com.start.Length == 1) gen.WriteLine("\t\t\t\t\tlevel := post_inc !level; x.NextCh();"); else { gen.WriteLine("\t\t\t\t\tx.NextCh();"); gen.WriteLine("\t\t\t\t\tif ({0}) then (", ChCond(com.start[1])); gen.WriteLine("\t\t\t\t\t\t level := post_inc !level; x.NextCh();"); gen.WriteLine("\t\t\t\t\t)"); } } gen.WriteLine("\t\t\t\t) else x.NextCh();"); gen.WriteLine("\t\t\t\t done;"); gen.WriteLine("\t\t\t\t !result;"); }