jroesch commented on a change in pull request #5932:
URL: https://github.com/apache/incubator-tvm/pull/5932#discussion_r448252018



##########
File path: src/parser/tokenizer.h
##########
@@ -0,0 +1,460 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*!
+ * \file parser.h
+ * \brief A parser for TVM IR.
+ */
+#ifndef TVM_PARSER_TOKENIZER_H_
+#define TVM_PARSER_TOKENIZER_H_
+
+#include <fstream>
+#include <tvm/runtime/object.h>
+#include <tvm/runtime/container.h>
+
+#include "./token.h"
+
+namespace tvm {
+namespace parser {
+
+using namespace runtime;
+
+bool IsDigit(char c) {
+    return '0' <= c && c <= '9';
+}
+
+bool IsWhitespace(char c) {
+    return ' ' == c || c == '\t' || c == '\n';
+}
+
+bool IsNumeric(char c) {
+    return (IsDigit(c) || c == '.' || c == 'e' || c == '-' || c == '+' || c == 
'E') && !IsWhitespace(c);
+}
+
+bool IsIdentLetter(char c) {
+    return '_' == c || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
+}
+
+bool IsIdent(char c) {
+    return IsIdentLetter(c) || IsDigit(c);
+}
+
+static std::unordered_map<std::string, TokenType> KEYWORD_TABLE = {
+    { "let", TokenType::Let },
+    { "fn", TokenType::Fn },
+    { "def", TokenType::Defn },
+    { "if", TokenType::If },
+    { "else", TokenType::Else },
+    { "type", TokenType::TypeDef },
+    { "match", TokenType::Match }
+};
+
+struct Tokenizer {
+    int pos;
+    int col;
+    int line;
+    char next_char;
+    const std::string& source;
+    std::vector<Token> tokens;
+
+    char Next() {
+        char c = this->source.at(this->pos);
+        if (c == '\n') {
+            this->line += 1;
+            this->col = 1;
+        } else {
+            this->col += 1;
+        }
+        pos += 1;
+        return c;
+    }
+
+    bool More() {
+        return this->pos < this->source.size();
+    }
+
+    char Peek() {
+        CHECK(pos < this->source.size());
+        return this->source.at(this->pos);
+    }
+
+    Token NewToken(TokenType token_type, ObjectRef data = ObjectRef()) {
+        return Token(this->line, this->col, token_type, data);
+    }
+
+    enum CommentParserState {
+        Proceed,
+        Forward,
+        Backward,
+    };
+
+    void MatchComment(std::string& buffer) {
+        // We only invoke this after we have matched the first start
+        // token assume, we are proceeding the parse forward with
+        // nesting = 1.
+        //
+        // When we are done we should be at nesting zero and be
+        // in the stop state.
+        CommentParserState state = CommentParserState::Proceed;
+        int nesting = 1;
+
+        while (true) {
+            switch (state) {
+                case CommentParserState::Proceed: {
+                    if (Peek() == '/') {
+                        state = CommentParserState::Forward;
+                    } else if (Peek() == '*') {
+                        state = CommentParserState::Backward;
+                    }
+                    buffer += Next();
+                    continue;
+                }
+                case CommentParserState::Forward: {
+                    if (Peek() == '*') {
+                        nesting += 1;
+                        buffer += Next();
+                    }
+                    state = CommentParserState::Proceed;
+                    continue;
+                }
+                case CommentParserState::Backward: {
+                    if (Peek() == '/') {
+                        nesting -= 1;
+                        if (nesting == 0) {
+                            Next();
+                            buffer.pop_back();
+                            return;
+                        } else {
+                            buffer += Next();
+                            state = CommentParserState::Proceed;
+                        };
+                    }
+                    continue;
+                }
+            }
+        }
+    }
+
+    Token ParseNumber(bool is_pos, std::string number) {
+        CHECK(number.size() > 0)
+            << "an empty string is an invalid number";
+
+        try {
+            auto token = NewToken(TokenType::Integer);
+            size_t index = 0;
+            int value = std::stoi(number, &index);
+            if (number.size() > index) {
+                throw std::invalid_argument("floating point");
+            }
+            value = is_pos ? value : -value;
+            token->data = tvm::Integer(value);
+            return token;
+        } catch (const std::invalid_argument& ia) {
+            auto token = NewToken(TokenType::Float);
+
+            if (number.back() == 'f') {
+                number.pop_back();
+            }
+
+            double value = stod(number);
+            value = is_pos ? value : -value;
+            token->data = tvm::FloatImm(DataType::Float(64), value);
+            return token;
+        }
+    }
+
+    inline Token TokenizeOnce() {
+        auto next = Peek();
+        if (next == '\n') {
+            auto token = NewToken(TokenType::Newline);
+            Next();
+            return token;
+        } else if (this->Peek() == '\r' && this->Peek() == '\n') {

Review comment:
       there is actually a fix me comment below lol




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to