]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/DEVEL/mathml_editor/src/TLexerPush.cc
Initial revision
[helm.git] / helm / DEVEL / mathml_editor / src / TLexerPush.cc
diff --git a/helm/DEVEL/mathml_editor/src/TLexerPush.cc b/helm/DEVEL/mathml_editor/src/TLexerPush.cc
new file mode 100644 (file)
index 0000000..6b2acae
--- /dev/null
@@ -0,0 +1,82 @@
+
+#include "TLexerPush.hh"
+
+TLexerPush::TLexerPush()
+{
+  state = ACCEPT;
+}
+
+TToken
+TLexerPush::pop()
+{
+  if (tokens.empty()) throw EmptyBuffer();
+  else
+    {
+      TToken res = tokens.front();
+      tokens.pop_front();
+      if (tokens.size() == 1 && state == CONTROL) state = ACCEPT;
+      return res;
+    }
+}
+
+TToken
+TLexerPush::front() const
+{
+  if (tokens.empty()) throw EmptyBuffer();
+  else return tokens.front();
+}
+
+bool
+TLexerPush::empty() const
+{
+  return tokens.empty();
+}
+
+bool
+TLexerPush::pending() const
+{
+  return state == ESCAPE;
+}
+
+bool
+TLexerPush::ambiguous() const
+{
+  return tokens.size() == 1 && state == CONTROL;
+}
+
+void
+TLexerPush::push(TChar ch)
+{
+  switch (state)
+    {
+    case ACCEPT:
+      if (ch == '\\') state = ESCAPE;
+      else tokens.push_back(TToken(ch));
+      break;
+    case ESCAPE:
+      tokens.push_back(TToken(TToken::CONTROL, std::string(1, ch)));
+      if (isUnicodeAlpha(ch)) state = CONTROL;
+      else state = ACCEPT;
+      break;
+    case CONTROL:
+      if (ch == '\\') state = ESCAPE;
+      else if (isUnicodeAlpha(ch))
+       {
+         assert(!tokens.empty());
+         TToken& tok = tokens.back();
+         tok.value.push_back(ch);
+       }
+      else if (isUnicodeSpace(ch)) state = IGNORE_SPACE;
+      else
+       {
+         tokens.push_back(TToken(ch));
+         state = ACCEPT;
+       }
+      break;
+    case IGNORE_SPACE:
+      if (ch == '\\') state = ESCAPE;
+      else if (isUnicodeSpace(ch)) ;
+      else tokens.push_back(TToken(ch));
+      break;
+    }
+}