Skip to content

Commit

Permalink
cmd,kmparser,kmcheck: add conflict and reserved names warnings
Browse files Browse the repository at this point in the history
Now, Karmem will report with "Warnings" with the schema contains
potential conflicts with the names or uses reserved names.

It's possible to avoid it using `-s`.

Fixes #17
  • Loading branch information
inkeliz committed Sep 1, 2022
1 parent b08ab05 commit 351b998
Show file tree
Hide file tree
Showing 16 changed files with 1,882 additions and 53 deletions.
22 changes: 22 additions & 0 deletions cmd/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## cmd/karmem

This folder contains files related to the karmem cli tool. The `cmd/karmem` is equivalent of
`protoc` or `flatc`. That is designed to be used as a command line tool, in order to read
karmem files and generate code for the corresponding languages.

## Usage

You can run this program with `go run karmem.org/cmd/karmem help`.

## Structure

- kmcheck:
- This package will verify if the parsed karmem file (from the `kmparser` package) contains
any potential conflict, or use any or uses any deprecated features.
- kmgen:
- This generates code for multiple languages. That will read the parsed
file (from the `kmparser` package) and generates the code for the given language.
- kmparser:
- This package is responsible to read karmem schema file. That package is
language independent.
- main.go: The main file for the karmem cli tool (that uses `kmparser`, `kmgen` and `kmcheck`).
108 changes: 108 additions & 0 deletions cmd/karmem/kmcheck/assemblyscript.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package kmcheck

import (
"sync"

"karmem.org/cmd/karmem/kmparser"
)

func init() { RegisterValidator(NewAssemblyScript()) }

type AssemblyScript struct {
RestrictedWords *RestrictedWords
}

func (v *AssemblyScript) CheckStruct(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.StructData) {
v.RestrictedWords.CheckStruct(mutex, parsed, target)
}

func (v *AssemblyScript) CheckEnum(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.EnumData) {
v.RestrictedWords.CheckEnum(mutex, parsed, target)
}

func NewAssemblyScript() *AssemblyScript {
return &AssemblyScript{
RestrictedWords: &RestrictedWords{
Language: kmparser.LanguageAssemblyScript,
Rules: []WordRule{
NewMatchRule("await"),
NewMatchRule("break"),
NewMatchRule("case"),
NewMatchRule("catch"),
NewMatchRule("class"),
NewMatchRule("const"),
NewMatchRule("continue"),
NewMatchRule("debugger"),
NewMatchRule("default"),
NewMatchRule("delete"),
NewMatchRule("do"),
NewMatchRule("else"),
NewMatchRule("enum"),
NewMatchRule("export"),
NewMatchRule("extends"),
NewMatchRule("false"),
NewMatchRule("finally"),
NewMatchRule("for"),
NewMatchRule("function"),
NewMatchRule("if"),
NewMatchRule("import"),
NewMatchRule("in"),
NewMatchRule("of"),
NewMatchRule("instanceof"),
NewMatchRule("new"),
NewMatchRule("null"),
NewMatchRule("return"),
NewMatchRule("super"),
NewMatchRule("switch"),
NewMatchRule("this"),
NewMatchRule("throw"),
NewMatchRule("true"),
NewMatchRule("try"),
NewMatchRule("typeof"),
NewMatchRule("type"),
NewMatchRule("var"),
NewMatchRule("void"),
NewMatchRule("while"),
NewMatchRule("with"),
NewMatchRule("yield"),
NewMatchRule("let"),
NewMatchRule("static"),
NewMatchRule("as"),
NewMatchRule("any"),
NewMatchRule("set"),
NewMatchRule("from"),
NewMatchRule("constructor"),
NewMatchRule("module"),
NewMatchRule("require"),
NewMatchRule("implements"),
NewMatchRule("interface"),
NewMatchRule("package"),
NewMatchRule("private"),
NewMatchRule("protected"),
NewMatchRule("and"),
NewMatchRule("public"),
NewMatchRule("i8"),
NewMatchRule("i16"),
NewMatchRule("i32"),
NewMatchRule("i64"),
NewMatchRule("u8"),
NewMatchRule("u16"),
NewMatchRule("u32"),
NewMatchRule("u64"),
NewMatchRule("f32"),
NewMatchRule("f64"),
NewMatchRule("bool"),
NewMatchRule("boolean"),
NewMatchRule("isize"),
NewMatchRule("usize"),
NewMatchRule("v128"),
NewMatchRule("externref"),
NewMatchRule("funcref"),
NewMatchRule("string"),
NewMatchRule("number"),
NewMatchRule("symbol"),
NewMatchRule("undefined"),
},
},
}
}
83 changes: 83 additions & 0 deletions cmd/karmem/kmcheck/c.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package kmcheck

import (
"sync"

"karmem.org/cmd/karmem/kmparser"
)

func init() { RegisterValidator(NewC()) }

type C struct {
RestrictedWords *RestrictedWords
CollisionArraySuffix *CollisionArraySuffix
}

func (v *C) CheckStruct(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.StructData) {
v.RestrictedWords.CheckStruct(mutex, parsed, target)

}

func (v *C) CheckEnum(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.EnumData) {
v.RestrictedWords.CheckEnum(mutex, parsed, target)
}

func NewC() *C {
return &C{
RestrictedWords: &RestrictedWords{
Language: kmparser.LanguageC,
Rules: []WordRule{
NewMatchRule("auto"),
NewMatchRule("else"),
NewMatchRule("long"),
NewMatchRule("switch"),
NewMatchRule("break"),
NewMatchRule("enum"),
NewMatchRule("register"),
NewMatchRule("typedef"),
NewMatchRule("case"),
NewMatchRule("extern"),
NewMatchRule("return"),
NewMatchRule("union"),
NewMatchRule("char"),
NewMatchRule("float"),
NewMatchRule("short"),
NewMatchRule("unsigned"),
NewMatchRule("const"),
NewMatchRule("for"),
NewMatchRule("signed"),
NewMatchRule("void"),
NewMatchRule("continue"),
NewMatchRule("goto"),
NewMatchRule("sizeof"),
NewMatchRule("volatile"),
NewMatchRule("default"),
NewMatchRule("if"),
NewMatchRule("static"),
NewMatchRule("while"),
NewMatchRule("do"),
NewMatchRule("int"),
NewMatchRule("struct"),
NewMatchRule("double"),
NewMatchRegexRule("^int[0-9]+_t$"),
NewMatchRegexRule("^uint[0-9]+_t$"),
NewMatchRegexRule("^int_fast[0-9]+_t$"),
NewMatchRegexRule("^uint_fast[0-9]+_t$"),
NewMatchRegexRule("^int_least[0-9]+_t$"),
NewMatchRegexRule("^uint_least[0-9]+_t$"),
NewMatchRegexRule("true"),
NewMatchRegexRule("false"),
NewMatchRegexRule("null"),
NewMatchRegexRule("bool"),
},
},
CollisionArraySuffix: &CollisionArraySuffix{
Language: kmparser.LanguageC,
Rules: []WordRule{
NewMatchSuffix("size"),
NewMatchSuffix("pointer"),
NewMatchSuffix("length"),
},
},
}
}
155 changes: 155 additions & 0 deletions cmd/karmem/kmcheck/dotnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
package kmcheck

import (
"sync"

"karmem.org/cmd/karmem/kmparser"
)

func init() { RegisterValidator(NewDotNet()) }

type DotNet struct {
RestrictedWords *RestrictedWords
CollisionParentChildField *CollisionParentChildField
}

func NewDotNet() *DotNet {
return &DotNet{
RestrictedWords: &RestrictedWords{
Language: kmparser.LanguageDotnet,
Rules: []WordRule{
NewMatchRule("abstract"),
NewMatchRule("as"),
NewMatchRule("base"),
NewMatchRule("bool"),
NewMatchRule("break"),
NewMatchRule("byte"),
NewMatchRule("case"),
NewMatchRule("catch"),
NewMatchRule("char"),
NewMatchRule("checked"),
NewMatchRule("class"),
NewMatchRule("const"),
NewMatchRule("continue"),
NewMatchRule("decimal"),
NewMatchRule("default"),
NewMatchRule("delegate"),
NewMatchRule("do"),
NewMatchRule("double"),
NewMatchRule("else"),
NewMatchRule("enum"),
NewMatchRule("event"),
NewMatchRule("explicit"),
NewMatchRule("extern"),
NewMatchRule("false"),
NewMatchRule("finally"),
NewMatchRule("fixed"),
NewMatchRule("float"),
NewMatchRule("for"),
NewMatchRule("foreach"),
NewMatchRule("goto"),
NewMatchRule("if"),
NewMatchRule("implicit"),
NewMatchRule("in"),
NewMatchRule("int"),
NewMatchRule("interface"),
NewMatchRule("internal"),
NewMatchRule("is"),
NewMatchRule("lock"),
NewMatchRule("long"),
NewMatchRule("namespace"),
NewMatchRule("new"),
NewMatchRule("null"),
NewMatchRule("object"),
NewMatchRule("operator"),
NewMatchRule("out"),
NewMatchRule("override"),
NewMatchRule("params"),
NewMatchRule("private"),
NewMatchRule("protected"),
NewMatchRule("public"),
NewMatchRule("readonly"),
NewMatchRule("ref"),
NewMatchRule("return"),
NewMatchRule("sbyte"),
NewMatchRule("sealed"),
NewMatchRule("short"),
NewMatchRule("sizeof"),
NewMatchRule("stackalloc"),
NewMatchRule("static"),
NewMatchRule("string"),
NewMatchRule("struct"),
NewMatchRule("switch"),
NewMatchRule("this"),
NewMatchRule("throw"),
NewMatchRule("true"),
NewMatchRule("try"),
NewMatchRule("typeof"),
NewMatchRule("uint"),
NewMatchRule("ulong"),
NewMatchRule("unchecked"),
NewMatchRule("unsafe"),
NewMatchRule("ushort"),
NewMatchRule("using"),
NewMatchRule("virtual"),
NewMatchRule("void"),
NewMatchRule("volatile"),
NewMatchRule("while"),
NewMatchRule("add"),
NewMatchRule("and"),
NewMatchRule("alias"),
NewMatchRule("ascending"),
NewMatchRule("args"),
NewMatchRule("async"),
NewMatchRule("await"),
NewMatchRule("by"),
NewMatchRule("descending"),
NewMatchRule("dynamic"),
NewMatchRule("equals"),
NewMatchRule("from"),
NewMatchRule("get"),
NewMatchRule("global"),
NewMatchRule("group"),
NewMatchRule("init"),
NewMatchRule("into"),
NewMatchRule("join"),
NewMatchRule("let"),
NewMatchRule("managed"),
NewMatchRule("nameof"),
NewMatchRule("nint"),
NewMatchRule("not"),
NewMatchRule("notnull"),
NewMatchRule("nuint"),
NewMatchRule("on"),
NewMatchRule("or"),
NewMatchRule("orderby"),
NewMatchRule("partial"),
NewMatchRule("record"),
NewMatchRule("remove"),
NewMatchRule("required"),
NewMatchRule("select"),
NewMatchRule("set"),
NewMatchRule("unmanage"),
NewMatchRule("value"),
NewMatchRule("var"),
NewMatchRule("when "),
NewMatchRule("where"),
NewMatchRule("with"),
NewMatchRule("yield"),
},
},
CollisionParentChildField: &CollisionParentChildField{
Language: kmparser.LanguageDotnet,
},
}
}

func (v *DotNet) CheckStruct(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.StructData) {
v.RestrictedWords.CheckStruct(mutex, parsed, target)
v.CollisionParentChildField.CheckStruct(mutex, parsed, target)
}

func (v *DotNet) CheckEnum(mutex *sync.Mutex, parsed *kmparser.Content, target *kmparser.EnumData) {
v.RestrictedWords.CheckEnum(mutex, parsed, target)
v.CollisionParentChildField.CheckEnum(mutex, parsed, target)
}
Loading

0 comments on commit 351b998

Please sign in to comment.