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 2, 2022
1 parent b08ab05 commit 2729b9a
Show file tree
Hide file tree
Showing 25 changed files with 2,283 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"),
},
},
}
}
38 changes: 38 additions & 0 deletions cmd/karmem/kmcheck/assemblyscript_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package kmcheck

import (
"testing"
)

func TestAssemblyScript(t *testing.T) {
testValidation(t, NewAssemblyScript(), `karmem test @packed(true);
enum TestEnum uint32 {
None;
One;
}
struct TestStruct table {
Foo int32;
Bar int16;
}
enum Package uint32 @error() {
None;
One;
}
enum TestEnumValid uint32 {
None;
Private;
}
struct TestStructErr table {
And []char @error();
}
struct From table @error() {
Foo []char;
}
`)
}
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)
v.CollisionArraySuffix.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"),
},
},
}
}
47 changes: 47 additions & 0 deletions cmd/karmem/kmcheck/c_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package kmcheck

import (
"testing"
)

func TestC(t *testing.T) {
testValidation(t, NewC(), `karmem test @packed(true);
enum TestEnum uint32 {
None;
One;
}
struct TestStruct table {
Foo int32;
Bar int16;
}
enum SizeOf uint32 @error() {
None;
One;
}
enum TestEnumValid uint32 {
None;
If;
}
struct TestStructErr table {
While []char @error();
}
struct Static table @error() {
Foo []char;
}
struct SomethingErr table {
Foo []char;
FooLength int32 @error();
}
struct Something table {
FooLength int32;
}
`)
}
Loading

0 comments on commit 2729b9a

Please sign in to comment.