All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dingyi222666.monarch.languages.LanguageProtobuf.kt Maven / Gradle / Ivy

The newest version!
package io.github.dingyi222666.monarch.languages

import io.github.dingyi222666.monarch.common.*
 import io.github.dingyi222666.monarch.loader.dsl.*
import io.github.dingyi222666.monarch.types.IMonarchLanguage

public val ProtobufLanguage: IMonarchLanguage by lazy {
  buildMonarchLanguage {
    tokenPostfix = ".proto"
    defaultToken = ""
    brackets {
      bracket("{","}","delimiter.curly")
      bracket("[","]","delimiter.square")
      bracket("(",")","delimiter.parenthesis")
      bracket("<",">","delimiter.angle")
    }
    symbols("[=>".action {
          token = "@brackets"
          switchTo = "identifier"
          bracket = "@close"
        }
      }
      "field" rules {
        include("@whitespace")
        "group".action {
          cases {
            "${'$'}S2==proto2" and {
              token = "keyword"
              switchTo = "@groupDecl.${'$'}S2"
            }
          }
        }
        "(@identifier)(\\s*)(=)".actionArray {
          token("identifier")
          token("white")
          action("delimiter") {
            next = "@pop"
          }
        }
        "@fullIdentifier|\\.".action {
          cases {
            "@builtinTypes" and "keyword"
            "@default" and "type.identifier"
          }
        }
      }
      "groupDecl" rules {
        include("@whitespace")
        "@identifier".token("identifier")
        "=".token("operator")
        "{".action {
          token = "@brackets"
          switchTo = "@messageBody.${'$'}S2"
          bracket = "@open"
        }
        include("@constant")
      }
      "type" rules {
        include("@whitespace")
        "@identifier".action("type.identifier").state("@pop")
        ".".token("delimiter")
      }
      "identifier" rules {
        include("@whitespace")
        "@identifier".action("identifier").state("@pop")
      }
      "serviceDecl" rules {
        include("@whitespace")
        "@identifier".token("identifier")
        "{".action {
          token = "@brackets"
          switchTo = "@serviceBody.${'$'}S2"
          bracket = "@open"
        }
      }
      "serviceBody" rules {
        include("@whitespace")
        include("@constant")
        ";".token("delimiter")
        "option\\b".action("keyword").state("@option.${'$'}S2")
        "rpc\\b".action("keyword").state("@rpc.${'$'}S2")
        "\\[".action {
          token = "@brackets"
          next = "@options.${'$'}S2"
          bracket = "@open"
        }
        "}".action {
          token = "@brackets"
          next = "@pop"
          bracket = "@close"
        }
      }
      "rpc" rules {
        include("@whitespace")
        "@identifier".token("identifier")
        "\\(".action {
          token = "@brackets"
          switchTo = "@request.${'$'}S2"
          bracket = "@open"
        }
        "{".action {
          token = "@brackets"
          next = "@methodOptions.${'$'}S2"
          bracket = "@open"
        }
        ";".action("delimiter").state("@pop")
      }
      "request" rules {
        include("@whitespace")
        "@messageType".action {
          cases {
            "stream" and {
              token = "keyword"
              next = "@type.${'$'}S2"
            }
            "@default" and "type.identifier"
          }
        }
        "\\)".action {
          token = "@brackets"
          switchTo = "@returns.${'$'}S2"
          bracket = "@close"
        }
      }
      "returns" rules {
        include("@whitespace")
        "returns\\b".token("keyword")
        "\\(".action {
          token = "@brackets"
          switchTo = "@response.${'$'}S2"
          bracket = "@open"
        }
      }
      "response" rules {
        include("@whitespace")
        "@messageType".action {
          cases {
            "stream" and {
              token = "keyword"
              next = "@type.${'$'}S2"
            }
            "@default" and "type.identifier"
          }
        }
        "\\)".action {
          token = "@brackets"
          switchTo = "@rpc.${'$'}S2"
          bracket = "@close"
        }
      }
      "methodOptions" rules {
        include("@whitespace")
        include("@constant")
        ";".token("delimiter")
        "option".token("keyword")
        "@optionName".token("annotation")
        "[()]".token("annotation.brackets")
        "=".token("operator")
        "}".action {
          token = "@brackets"
          next = "@pop"
          bracket = "@close"
        }
      }
      comment {
        "[^\\/*]+".token("comment")
        "\\/\\*".action("comment").state("@push")
        "\\*/".action("comment").state("@pop")
        "[\\/*]".token("comment")
      }
      string {
        "[^\\\\\"]+".token("string")
        "@escapes".token("string.escape")
        "\\\\.".token("string.escape.invalid")
        "\"".action {
          token = "string.quote"
          next = "@pop"
          bracket = "@close"
        }
      }
      "stringSingle" rules {
        "[^\\\\']+".token("string")
        "@escapes".token("string.escape")
        "\\\\.".token("string.escape.invalid")
        "'".action {
          token = "string.quote"
          next = "@pop"
          bracket = "@close"
        }
      }
      "constant" rules {
        "@boolLit".token("keyword.constant")
        "@hexLit".token("number.hex")
        "@octalLit".token("number.octal")
        "@decimalLit".token("number")
        "@floatLit".token("number.float")
        "(\"([^\"\\\\]|\\\\.)*|'([^'\\\\]|\\\\.)*)${'$'}".token("string.invalid")
        "\"".action {
          token = "string.quote"
          next = "@string"
          bracket = "@open"
        }
        "'".action {
          token = "string.quote"
          next = "@stringSingle"
          bracket = "@open"
        }
        "{".action {
          token = "@brackets"
          next = "@prototext"
          bracket = "@open"
        }
        "identifier".token("identifier")
      }
      whitespace {
        "[ \\t\\r\\n]+".token("white")
        "\\/\\*".action("comment").state("@comment")
        "\\/\\/.*${'$'}".token("comment")
      }
      "prototext" rules {
        include("@whitespace")
        include("@constant")
        "@identifier".token("identifier")
        "[:;]".token("delimiter")
        "}".action {
          token = "@brackets"
          next = "@pop"
          bracket = "@close"
        }
      }
    }
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy