From 13f074686970cf037cbd8a4c492033bb7b14562d Mon Sep 17 00:00:00 2001 From: Bilyal Mestanov Date: Wed, 7 Nov 2018 14:49:11 +0200 Subject: Add homework 1 (#23) --- homework/81270/js2html/Highlighter.cpp | 71 ++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 homework/81270/js2html/Highlighter.cpp (limited to 'homework/81270/js2html/Highlighter.cpp') diff --git a/homework/81270/js2html/Highlighter.cpp b/homework/81270/js2html/Highlighter.cpp new file mode 100644 index 0000000..cd56496 --- /dev/null +++ b/homework/81270/js2html/Highlighter.cpp @@ -0,0 +1,71 @@ +// +// Created by bmestanov on 25.10.18. +// + +#include "Highlighter.h" +#include +#include +#include + +Highlighter::Highlighter(const char *outputPath, const char *stylePath) : + outputPath(outputPath), stylePath(stylePath) {} + +void Highlighter::highlight(const IPLVector &tokens) { + std::stringstream ss; + + ss << "\n"; + ss << "\n"; + ss << " \n"; + ss << " hello.js\n"; + ss << " \n"; + ss << " \n"; + ss << " \n"; + ss << "
\n";
+
+    for (auto &token : tokens) {
+        IPLString cssClass = getClass(token);
+        if (cssClass.empty()) {
+            // Print with no formatting
+            ss << token.lexeme;
+        } else {
+            ss << "" << token.lexeme << "";
+        }
+    }
+
+    ss << " \n";
+    ss << "\n";
+
+    std::ofstream fs(outputPath);
+    if (fs) {
+        fs << ss.rdbuf();
+    } else {
+        std::cout << ss.rdbuf();
+    }
+}
+
+IPLString Highlighter::getClass(const Token &token) {
+    switch (token.type) {
+        case Whitespace:
+        case NewLine:
+        case Raw:
+            return "";
+        case Comment:
+            return "comment";
+        case Keyword:
+            return "keyword";
+        case NumericLiteral:
+            return "numeric";
+        case StringLiteral:
+            return "string";
+        case TemplateLiteral:
+            return "template";
+        case Regex:
+            return "regex";
+        case Identifier:
+            return "identifier";
+        case BooleanLiteral:
+            return "boolean";
+        case Global:
+            return "global";
+    }
+}
-- 
cgit v1.3.1-23-gd82d