Browse Source

Support both tabbed and spaces indents for TB ui files (not mixed in same file)

Josh Engebretson 9 years ago
parent
commit
c28c6c4340

+ 53 - 3
Source/ThirdParty/TurboBadger/parser/tb_parser.cpp

@@ -252,11 +252,61 @@ void TBParser::OnLine(char *line, TBParserTarget *target)
         return;
     }
 
+    int i = 0;
+    int tabs = 0;
+    int spaces = 0;
+
+    while (line[i] != 0)
+    {
+        if (line[i] == '\t')
+            tabs++;
+        else if (line[i] == ' ')
+            spaces++;
+        else
+            break;
+        i++;
+    }
+
+    if (spaces && indent_spaces == -1)
+    {
+        indent_spaces = spaces;
+    }
+
+    if ((tabs || indent_tabs) && spaces)
+    {
+        target->OnError(current_line_nr, "Indentation error. Mixed tabs and spaces (Line skipped)");
+        return;
+    }
+
+
     // Check indent
     int indent = 0;
-    while (line[indent] == '\t' && line[indent] != 0)
-        indent++;
-    line += indent;
+
+    if (tabs)
+    {
+        indent_tabs = true;
+        indent += tabs;
+        line += tabs;
+    }
+    else
+    {
+        i = 0;
+        int c = 0;
+        while (line[i] == ' ' && line[i] != 0)
+        {
+            c++;
+            i++;
+
+            if (indent_spaces == c)
+            {
+                c = 0;
+                indent++;
+            }
+
+        }
+
+        line += i;
+    }
 
     if (indent - current_indent > 1)
     {

+ 4 - 1
Source/ThirdParty/TurboBadger/parser/tb_parser.h

@@ -47,7 +47,7 @@ public:
         STATUS_OUT_OF_MEMORY,
         STATUS_PARSE_ERROR
     };
-    TBParser() {}
+    TBParser() { indent_spaces = -1; indent_tabs = false; }
     STATUS Read(TBParserStream *stream, TBParserTarget *target);
 private:
     int current_indent;
@@ -60,6 +60,9 @@ private:
     void OnCompactLine(char *line, TBParserTarget *target);
     void OnMultiline(char *line, TBParserTarget *target);
     void ConsumeValue(TBValue &dst_value, char *&line);
+
+    int indent_spaces;
+    bool indent_tabs;
 };
 
 }; // namespace tb