Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler fixes #4215

Merged
merged 7 commits into from Aug 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Expand Up @@ -151,7 +151,7 @@ If you are the current maintainer of this gem:
1. Bump the Gemfile and Gemfile.lock versions for an app which relies on this gem
1. Install the new gem locally
1. Test behavior locally, branch deploy, whatever needs to happen
1. Bump gem version in `lib/linguist/version.rb`, [like this](https://github.com/github/linguist/commit/8d2ea90a5ba3b2fe6e1508b7155aa4632eea2985).
1. Bump gem version in `lib/linguist/VERSION`, [like this](https://github.com/github/linguist/commit/3212355400974ce5f7873a71eb8b85b1c5f4a6d2).
1. Make a PR to github/linguist, [like this](https://github.com/github/linguist/pull/1238).
1. Build a local gem: `bundle exec rake build_gem`
1. Merge github/linguist PR
Expand Down
1 change: 1 addition & 0 deletions lib/linguist/VERSION
@@ -0,0 +1 @@
6.4.0
2 changes: 1 addition & 1 deletion lib/linguist/version.rb
@@ -1,3 +1,3 @@
module Linguist
VERSION = "6.4.0"
VERSION = File.read(File.expand_path("../VERSION", __FILE__)).strip
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice. One less hack for the release script I've been slowly working on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet!

end
6 changes: 3 additions & 3 deletions tools/grammars/cmd/grammar-compiler/main.go
Expand Up @@ -40,7 +40,7 @@ func main() {
},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return wrap(err)
}
Expand All @@ -65,7 +65,7 @@ func main() {
},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return wrap(err)
}
Expand All @@ -91,7 +91,7 @@ func main() {
cli.StringFlag{Name: "out, o"},
},
Action: func(c *cli.Context) error {
conv, err := compiler.NewConverter(c.String("linguist-path"))
conv, err := compiler.NewConverter(c.GlobalString("linguist-path"))
if err != nil {
return cli.NewExitError(err, 1)
}
Expand Down
17 changes: 15 additions & 2 deletions tools/grammars/compiler/converter.go
Expand Up @@ -18,7 +18,8 @@ import (
)

type Converter struct {
root string
root string
version string

modified bool
grammars map[string][]string
Expand Down Expand Up @@ -194,6 +195,13 @@ func (conv *Converter) WriteJSON(rulePath string) error {
return err
}

f, err := os.Create(path.Join(rulePath, "version"))
if err != nil {
return err
}
f.Write([]byte(conv.version))
f.Close()

for _, repo := range conv.Loaded {
for scope, file := range repo.Files {
p := path.Join(rulePath, scope+".json")
Expand Down Expand Up @@ -249,12 +257,17 @@ func (conv *Converter) Report() error {
}

func NewConverter(root string) (*Converter, error) {
ver, err := ioutil.ReadFile(path.Join(root, "lib", "linguist", "VERSION"))
if err != nil {
return nil, err
}

yml, err := ioutil.ReadFile(path.Join(root, "grammars.yml"))
if err != nil {
return nil, err
}

conv := &Converter{root: root}
conv := &Converter{root: root, version: strings.TrimSpace(string(ver))}

if err := yaml.Unmarshal(yml, &conv.grammars); err != nil {
return nil, err
Expand Down
10 changes: 10 additions & 0 deletions tools/grammars/compiler/errors.go
Expand Up @@ -83,3 +83,13 @@ func (err *InvalidRegexError) Error() string {
"Invalid regex in grammar: %s contains a malformed regex (%s)",
err.File, err.Err)
}

type UndeclaredScopeError struct {
Path string
}

func (err *UndeclaredScopeError) Error() string {
return fmt.Sprintf(
"Undeclared scope in grammar: `%s` has no scope name",
err.Path)
}
2 changes: 1 addition & 1 deletion tools/grammars/compiler/loader_fs.go
Expand Up @@ -73,7 +73,7 @@ func (l *fsLoader) load() {
path = rel
}

rule, unknown, err := ConvertProto(filepath.Ext(path), data)
rule, unknown, err := ConvertProto(path, filepath.Ext(path), data)
if err != nil {
l.Fail(&ConversionError{path, err})
continue
Expand Down
4 changes: 2 additions & 2 deletions tools/grammars/compiler/loader_url.go
Expand Up @@ -41,7 +41,7 @@ func (l *urlLoader) loadTarball(r io.Reader) {
}

ext := filepath.Ext(header.Name)
rule, unknown, err := ConvertProto(ext, data)
rule, unknown, err := ConvertProto(header.Name, ext, data)
if err != nil {
l.Fail(&ConversionError{header.Name, err})
continue
Expand Down Expand Up @@ -77,7 +77,7 @@ func (l *urlLoader) load() {

ext := filepath.Ext(l.Source)
filename := filepath.Base(l.Source)
rule, unknown, err := ConvertProto(ext, data)
rule, unknown, err := ConvertProto(l.Source, ext, data)
if err != nil {
l.Fail(&ConversionError{filename, err})
return
Expand Down
6 changes: 5 additions & 1 deletion tools/grammars/compiler/proto.go
Expand Up @@ -49,7 +49,7 @@ func filterUnusedKeys(keys []string) (out []string) {
return
}

func ConvertProto(ext string, data []byte) (*grammar.Rule, []string, error) {
func ConvertProto(path, ext string, data []byte) (*grammar.Rule, []string, error) {
var (
raw map[string]interface{}
out grammar.Rule
Expand Down Expand Up @@ -92,5 +92,9 @@ func ConvertProto(ext string, data []byte) (*grammar.Rule, []string, error) {
return nil, nil, err
}

if out.ScopeName == "" {
return nil, nil, &UndeclaredScopeError{path}
}

return &out, filterUnusedKeys(md.Unused), nil
}