From 536a22c1b83500d4efaa7a1eae0e84ac8743b768 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Tue, 27 Oct 2015 12:10:54 -0700 Subject: 2287 - new lexing rules Now we always consider words to be terminated at () and {}. We also always skip commas. --- 011load.cc | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/011load.cc b/011load.cc index fceaff83..47829b04 100644 --- a/011load.cc +++ b/011load.cc @@ -122,7 +122,6 @@ bool next_instruction(istream& in, instruction* curr) { vector::iterator p = words.begin(); if (find(words.begin(), words.end(), "<-") != words.end()) { for (; *p != "<-"; ++p) { - if (*p == ",") continue; curr->products.push_back(reagent(*p)); } ++p; // skip <- @@ -143,7 +142,6 @@ bool next_instruction(istream& in, instruction* curr) { curr->operation = Recipe_ordinal[*p]; ++p; for (; p != words.end(); ++p) { - if (*p == ",") continue; curr->ingredients.push_back(reagent(*p)); } @@ -163,6 +161,7 @@ bool next_instruction(istream& in, instruction* curr) { string next_word(istream& in) { skip_whitespace(in); + skip_ignored_characters(in); // End next_word Special-cases ostringstream out; slurp_word(in, out); @@ -171,16 +170,20 @@ string next_word(istream& in) { return out.str(); } +:(before "End Globals") +// word boundaries +string Terminators("(){}"); +string Ignore(","); // meaningless except within [] strings +:(code) void slurp_word(istream& in, ostream& out) { - static string terminators(",()[]{}"); char c; - if (!in.eof() && terminators.find(in.peek()) != string::npos) { + if (!in.eof() && Terminators.find(in.peek()) != string::npos) { in >> c; out << c; return; } while (in >> c) { - if (isspace(c) || terminators.find(c) != string::npos) { + if (isspace(c) || Terminators.find(c) != string::npos || Ignore.find(c) != string::npos) { in.putback(c); break; } @@ -188,6 +191,12 @@ void slurp_word(istream& in, ostream& out) { } } +void skip_ignored_characters(istream& in) { + while (isspace(in.peek()) || Ignore.find(in.peek()) != string::npos) { + in.get(); + } +} + void skip_whitespace_and_comments(istream& in) { while (true) { if (in.eof()) break; -- cgit 1.4.1-2-gfad0