gob-lang 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/README.md +17 -0
  3. data/exe/gob +8 -0
  4. data/exe/stree-go +12 -0
  5. data/ext/gipper/gipper.go +172 -0
  6. data/ext/gipper/libgipper.h +95 -0
  7. data/ext/goformatter/format/LICENSE +27 -0
  8. data/ext/goformatter/format/format.go +132 -0
  9. data/ext/goformatter/format/internal.go +174 -0
  10. data/ext/goformatter/go.mod +3 -0
  11. data/ext/goformatter/libgoformatter.h +95 -0
  12. data/ext/goformatter/main.go +34 -0
  13. data/gob-lang.gemspec +57 -0
  14. data/lib/gipper.rb +24 -0
  15. data/lib/go_formatter.rb +33 -0
  16. data/lib/gob/cli/base.rb +53 -0
  17. data/lib/gob/cli/commands/build.rb +29 -0
  18. data/lib/gob/cli/commands/fmt.rb +31 -0
  19. data/lib/gob/cli/commands/run.rb +21 -0
  20. data/lib/gob/cli/commands/transpile.rb +20 -0
  21. data/lib/gob/cli/commands/version.rb +19 -0
  22. data/lib/gob/cli/commands/vet.rb +33 -0
  23. data/lib/gob/cli/errors.rb +50 -0
  24. data/lib/gob/cli.rb +53 -0
  25. data/lib/gob/go/dsl.rb +196 -0
  26. data/lib/gob/go/printer.rb +412 -0
  27. data/lib/gob/go/runner.rb +42 -0
  28. data/lib/gob/matcher.rb +34 -0
  29. data/lib/gob/transpilation/rules/core.rb +1184 -0
  30. data/lib/gob/transpiler.rb +815 -0
  31. data/lib/gob/validation/offence.rb +24 -0
  32. data/lib/gob/validation/rules/class_rules.rb +78 -0
  33. data/lib/gob/validation/rules/config.yml +16 -0
  34. data/lib/gob/validation/rules/cop.rb +15 -0
  35. data/lib/gob/validation/rules/function_rules.rb +33 -0
  36. data/lib/gob/validation/rules/module_rules.rb +78 -0
  37. data/lib/gob/validation/rules/program_rules.rb +72 -0
  38. data/lib/gob/validation/rules.rb +93 -0
  39. data/lib/gob/version.rb +5 -0
  40. data/lib/gob.rb +41 -0
  41. data/lib/syntax_tree/go/format.rb +176 -0
  42. data/lib/syntax_tree/go/json_visitor.rb +34 -0
  43. data/lib/syntax_tree/go/match_visitor.rb +103 -0
  44. data/lib/syntax_tree/go/materializer.rb +92 -0
  45. data/lib/syntax_tree/go/node.rb +50 -0
  46. data/lib/syntax_tree/go/nodes.rb +2147 -0
  47. data/lib/syntax_tree/go/pretty_print_visitor.rb +76 -0
  48. data/lib/syntax_tree/go/schema.json +1455 -0
  49. data/lib/syntax_tree/go/visitor.rb +71 -0
  50. data/lib/syntax_tree/go.rb +55 -0
  51. data/lib/thor/help_header.rb +23 -0
  52. metadata +290 -0
@@ -0,0 +1,95 @@
1
+ /* Code generated by cmd/cgo; DO NOT EDIT. */
2
+
3
+ /* package gob.ext/goformatter */
4
+
5
+
6
+ #line 1 "cgo-builtin-export-prolog"
7
+
8
+ #include <stddef.h>
9
+
10
+ #ifndef GO_CGO_EXPORT_PROLOGUE_H
11
+ #define GO_CGO_EXPORT_PROLOGUE_H
12
+
13
+ #ifndef GO_CGO_GOSTRING_TYPEDEF
14
+ typedef struct { const char *p; ptrdiff_t n; } _GoString_;
15
+ extern size_t _GoStringLen(_GoString_ s);
16
+ extern const char *_GoStringPtr(_GoString_ s);
17
+ #endif
18
+
19
+ #endif
20
+
21
+ /* Start of preamble from import "C" comments. */
22
+
23
+
24
+ #line 3 "main.go"
25
+
26
+ #include <stdlib.h>
27
+
28
+ #line 1 "cgo-generated-wrapper"
29
+
30
+
31
+ /* End of preamble from import "C" comments. */
32
+
33
+
34
+ /* Start of boilerplate cgo prologue. */
35
+ #line 1 "cgo-gcc-export-header-prolog"
36
+
37
+ #ifndef GO_CGO_PROLOGUE_H
38
+ #define GO_CGO_PROLOGUE_H
39
+
40
+ typedef signed char GoInt8;
41
+ typedef unsigned char GoUint8;
42
+ typedef short GoInt16;
43
+ typedef unsigned short GoUint16;
44
+ typedef int GoInt32;
45
+ typedef unsigned int GoUint32;
46
+ typedef long long GoInt64;
47
+ typedef unsigned long long GoUint64;
48
+ typedef GoInt64 GoInt;
49
+ typedef GoUint64 GoUint;
50
+ typedef size_t GoUintptr;
51
+ typedef float GoFloat32;
52
+ typedef double GoFloat64;
53
+ #ifdef _MSC_VER
54
+ #if !defined(__cplusplus) || _MSVC_LANG <= 201402L
55
+ #include <complex.h>
56
+ typedef _Fcomplex GoComplex64;
57
+ typedef _Dcomplex GoComplex128;
58
+ #else
59
+ #include <complex>
60
+ typedef std::complex<float> GoComplex64;
61
+ typedef std::complex<double> GoComplex128;
62
+ #endif
63
+ #else
64
+ typedef float _Complex GoComplex64;
65
+ typedef double _Complex GoComplex128;
66
+ #endif
67
+
68
+ /*
69
+ static assertion to make sure the file is being used on architecture
70
+ at least with matching size of GoInt.
71
+ */
72
+ typedef char _check_for_64_bit_pointer_matching_GoInt[sizeof(void*)==64/8 ? 1:-1];
73
+
74
+ #ifndef GO_CGO_GOSTRING_TYPEDEF
75
+ typedef _GoString_ GoString;
76
+ #endif
77
+ typedef void *GoMap;
78
+ typedef void *GoChan;
79
+ typedef struct { void *t; void *v; } GoInterface;
80
+ typedef struct { void *data; GoInt len; GoInt cap; } GoSlice;
81
+
82
+ #endif
83
+
84
+ /* End of boilerplate cgo prologue. */
85
+
86
+ #ifdef __cplusplus
87
+ extern "C" {
88
+ #endif
89
+
90
+ extern char* FormatSourceJSON(char* src);
91
+ extern void FreeString(char* str);
92
+
93
+ #ifdef __cplusplus
94
+ }
95
+ #endif
@@ -0,0 +1,34 @@
1
+ package main
2
+
3
+ /*
4
+ #include <stdlib.h>
5
+ */
6
+ import "C"
7
+
8
+ import (
9
+ "encoding/json"
10
+ "unsafe"
11
+
12
+ "gob.ext/goformatter/format"
13
+ )
14
+
15
+ //export FormatSourceJSON
16
+ func FormatSourceJSON(src *C.char) *C.char {
17
+ output, err := format.Source([]byte(C.GoString(src)))
18
+ result := map[string]any{"output": string(output)}
19
+ if err != nil {
20
+ result["error"] = err.Error()
21
+ }
22
+
23
+ encoded, _ := json.Marshal(result)
24
+ return C.CString(string(encoded))
25
+ }
26
+
27
+ //export FreeString
28
+ func FreeString(str *C.char) {
29
+ if str != nil {
30
+ C.free(unsafe.Pointer(str))
31
+ }
32
+ }
33
+
34
+ func main() {}
data/gob-lang.gemspec ADDED
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+ # rubocop:disable all
3
+
4
+ require_relative "lib/gob/version"
5
+
6
+ if defined?(GemKit::Release)
7
+ GemKit::Release.configure do |config|
8
+ config.version_file = "lib/gob/version.rb"
9
+ config.require_path = "gob"
10
+ end
11
+ end
12
+
13
+ Gem::Specification.new do |spec|
14
+ spec.name = "gob-lang"
15
+ spec.version = Gob::VERSION
16
+ spec.authors = ["Nathan Kidd"]
17
+ spec.email = ["nathankidd@hey.com"]
18
+ spec.license = "Apache-2.0"
19
+ spec.summary = "Gobble gobble, little turkey."
20
+ spec.description = "Don't even worry about it... it's not for you."
21
+ spec.homepage = "https://github.com/n-at-han-k/gob"
22
+ spec.required_ruby_version = ">= 3.3"
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+ spec.metadata["documentation_uri"] = "#{spec.homepage}#readme"
25
+
26
+ spec.platform = Gem::Platform.new(ENV["GOB_NATIVE_PLATFORM"]) if ENV["GOB_NATIVE_PLATFORM"]
27
+
28
+ spec.files = Dir.glob(
29
+ ["{lib,exe}/**/*", "{ext}/**/*", "README.md", "gob-lang.gemspec"],
30
+ File::FNM_DOTMATCH,
31
+ base: __dir__
32
+ )
33
+
34
+ unless ENV["GOB_NATIVE_PLATFORM"]
35
+ spec.files.reject! do |path|
36
+ File.fnmatch?("*.{so,bundle,dll,dylib,o,a}", path, File::FNM_EXTGLOB)
37
+ end
38
+ end
39
+
40
+ spec.bindir = "exe"
41
+ spec.executables = Dir["exe/*"].map { |path| File.basename(path) }.sort
42
+ spec.require_paths = ["lib"]
43
+ spec.add_dependency "syntax_tree", "~> 6.3"
44
+ spec.add_dependency "ffi", "~> 1.17"
45
+ spec.add_dependency "thor", "~> 1.5"
46
+ spec.add_dependency "gem_kit", "~> 0.2"
47
+ spec.add_dependency "rubocop", "~> 1.92"
48
+ spec.add_development_dependency "ast-crispr", "~> 7.1"
49
+ spec.add_development_dependency "ast-crispr-ruby-prism", "~> 7.1"
50
+ spec.add_development_dependency "unparser", "~> 0.9"
51
+ spec.add_development_dependency "minitest", "~> 6.1"
52
+ spec.add_development_dependency "minitest-reporters", "~> 1.8"
53
+ spec.add_development_dependency "diff-lcs", "~> 2.0"
54
+ spec.add_development_dependency "rubocop-nathank", "~> 2.1"
55
+ spec.add_development_dependency "rubocop-disable_syntax", "~> 0.2"
56
+ spec.add_development_dependency "gem_kit-release", "~> 0.4"
57
+ end
data/lib/gipper.rb ADDED
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ require "json"
5
+
6
+ module Gipper
7
+ extend FFI::Library
8
+
9
+ LIB = File.expand_path("../ext/gipper/libgipper.#{FFI::Platform::LIBSUFFIX}", __dir__)
10
+ ffi_lib LIB
11
+
12
+ attach_function :ParseFileJSON, [:string, :string], :pointer
13
+ attach_function :FreeString, [:pointer], :void
14
+
15
+ def self.parse_file(source, filename: "main.go")
16
+ ptr = ParseFileJSON(filename, source)
17
+ raise "Gipper.ParseFileJSON returned null" if ptr.null?
18
+
19
+ json = ptr.read_string.force_encoding("UTF-8")
20
+ JSON.parse(json)
21
+ ensure
22
+ FreeString(ptr) if ptr && !ptr.null?
23
+ end
24
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+ require "json"
5
+
6
+ module GoFormatter
7
+ class Error < StandardError; end
8
+
9
+ extend FFI::Library # rubocop:disable Layout/EmptyLinesAfterModuleInclusion, Transpile/BlankLineBetweenStatements
10
+ ffi_lib File.expand_path("../ext/goformatter/libgoformatter.#{FFI::Platform::LIBSUFFIX}", __dir__)
11
+ attach_function :FormatSourceJSON, [:string], :pointer
12
+ attach_function :FreeString, [:pointer], :void
13
+
14
+ def self.format(source)
15
+ pointer = FormatSourceJSON(source)
16
+
17
+ if pointer.null?
18
+ raise Error, "GoFormatter.FormatSourceJSON returned null"
19
+ end
20
+
21
+ result = JSON.parse(pointer.read_string.force_encoding("UTF-8"))
22
+
23
+ if result["error"]
24
+ raise Error, result.fetch("error")
25
+ end
26
+
27
+ result.fetch("output")
28
+ ensure
29
+ if pointer && !pointer.null?
30
+ FreeString(pointer)
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "gob"
5
+ require "go_formatter"
6
+
7
+ module Gob
8
+ class CLI
9
+ class Base < Thor
10
+ def self.exit_on_failure?
11
+ true
12
+ end
13
+
14
+ no_commands do
15
+ def read_source(path)
16
+ if path == "-"
17
+ [$stdin.read, "stdin"]
18
+ else
19
+ [File.read(path), path]
20
+ end
21
+ end
22
+
23
+ def files_from(*paths)
24
+ raise Thor::Error, "No files provided" if paths.empty?
25
+
26
+ paths
27
+ end
28
+
29
+ def validate_source!(source, path)
30
+ offenses = Gob::Validation::Rules.validate(source, path: path)
31
+ return if offenses.empty?
32
+
33
+ Gob::CLI::Errors.render_offenses(offenses, path: path)
34
+ exit 1
35
+ end
36
+
37
+ def transpile_source(source, path)
38
+ validate_source!(source, path)
39
+ ast = Gob::Transpiler.new.transpile(source)
40
+ GoFormatter.format(Gob::Go::Printer.print(ast))
41
+ end
42
+
43
+ def write_output(output, path)
44
+ if path.nil? || path == "-"
45
+ $stdout.write(output)
46
+ else
47
+ File.write(path, output)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+ require_relative "../../go/runner"
5
+
6
+ module Gob
7
+ class CLI
8
+ module Commands
9
+ class Build < Base
10
+ default_command :build
11
+ desc "build FILE", "Transpile and build a Gob program"
12
+ method_option :output, aliases: "-o", type: :string, desc: "Write binary to FILE"
13
+
14
+ def build(file)
15
+ source, path = read_source(file)
16
+ go_source = transpile_source(source, path)
17
+
18
+ if file == "-"
19
+ output = options[:output] || File.basename("gob", ".gob")
20
+ else
21
+ output = options[:output] || File.basename(file, ".gob")
22
+ end
23
+
24
+ exit 1 unless Gob::Go::Runner.build(go_source, output: output)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+
5
+ module Gob
6
+ class CLI
7
+ module Commands
8
+ class Fmt < Base
9
+ default_command :fmt
10
+ desc "fmt FILE...", "Format Gob source"
11
+ method_option :write, aliases: "-w", type: :boolean, default: false, desc: "Write formatted source back to FILE"
12
+
13
+ def fmt(*files)
14
+ files_from(*files)
15
+
16
+ files.each do |file|
17
+ source, = read_source(file)
18
+ formatted = SyntaxTree.format(source)
19
+
20
+ if options[:write]
21
+ raise Thor::Error, "Cannot use --write with stdin" if file == "-"
22
+ File.write(file, formatted)
23
+ else
24
+ $stdout.write(formatted)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+ require_relative "../../go/runner"
5
+
6
+ module Gob
7
+ class CLI
8
+ module Commands
9
+ class Run < Base
10
+ default_command :execute
11
+ desc "run FILE [ARGS...]", "Transpile and run a Gob program"
12
+
13
+ def execute(file, *args)
14
+ source, path = read_source(file)
15
+ go_source = transpile_source(source, path)
16
+ exit 1 unless Gob::Go::Runner.run(go_source, args: args)
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+
5
+ module Gob
6
+ class CLI
7
+ module Commands
8
+ class Transpile < Base
9
+ default_command :transpile
10
+ desc "transpile FILE", "Transpile Gob source to Go"
11
+ method_option :output, aliases: "-o", type: :string, desc: "Write Go source to FILE"
12
+
13
+ def transpile(file)
14
+ source, path = read_source(file)
15
+ write_output(transpile_source(source, path), options[:output])
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+ require_relative "../../version"
5
+
6
+ module Gob
7
+ class CLI
8
+ module Commands
9
+ class Version < Base
10
+ default_command :version
11
+ desc "version", "Print Gob version"
12
+
13
+ def version
14
+ puts Gob::VERSION
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../base"
4
+ require "rubocop/formatter/simple_text_formatter"
5
+
6
+ module Gob
7
+ class CLI
8
+ module Commands
9
+ class Vet < Base
10
+ default_command :vet
11
+ desc "vet FILE...", "Validate Gob source"
12
+
13
+ def vet(*files)
14
+ paths = files_from(*files).flat_map do |file|
15
+ if File.directory?(file)
16
+ matches = Dir[File.join(file, "**/*.gob")].sort
17
+ raise Thor::Error, "No .gob files found in #{file}" if matches.empty?
18
+
19
+ matches
20
+ else
21
+ [file]
22
+ end
23
+ end.uniq
24
+
25
+ entries = paths.map { |file| read_source(file) }
26
+ formatter = RuboCop::Formatter::SimpleTextFormatter.new($stdout)
27
+ offenses = Gob::Validation::Rules.validate_files(entries, formatter: formatter)
28
+ exit 1 unless offenses.empty?
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Gob
4
+ class CLI
5
+ module Errors
6
+ module_function
7
+
8
+ def render_offenses(offenses, path: nil)
9
+ offenses.each do |offense|
10
+ filename = path || "gob"
11
+
12
+ if offense.column
13
+ column = offense.column + 1
14
+ else
15
+ column = nil
16
+ end
17
+
18
+ location = [filename, offense.line, column].compact.join(":")
19
+ message = offense.message.sub(/\A#{Regexp.escape(offense.cop_name)}:\s*/, "")
20
+ warn "#{location}: C: #{offense.cop_name}: #{message}"
21
+ end
22
+ end
23
+
24
+ def render(error)
25
+ case error
26
+ when Gob::Validation::Offence
27
+ path = error.path || "gob"
28
+
29
+ if error.column
30
+ column = error.column + 1
31
+ else
32
+ column = nil
33
+ end
34
+
35
+ location = [path, error.line, column].compact.join(":")
36
+ message = error.message_text.sub(/\A#{Regexp.escape(error.cop)}:\s*/, "")
37
+ warn "#{location}: C: #{error.cop}: #{message}"
38
+ when SyntaxTree::Parser::ParseError
39
+ warn "gob: #{error.message}"
40
+ when Gob::Transpiler::UnsupportedNode
41
+ warn "gob: #{error.message}"
42
+ when GoFormatter::Error
43
+ warn "gob: #{error.message}"
44
+ else
45
+ warn "gob: #{error.message}"
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
data/lib/gob/cli.rb ADDED
@@ -0,0 +1,53 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "thor"
4
+ require "thor/help_header"
5
+
6
+ module Gob
7
+ class CLI < Thor
8
+ def self.exit_on_failure?
9
+ true
10
+ end
11
+
12
+ def self.run(argv = ARGV)
13
+ start(argv)
14
+ 0
15
+ rescue StandardError => error
16
+ Errors.render(error)
17
+ 1
18
+ end
19
+ end
20
+ end
21
+
22
+ require_relative "version"
23
+ require_relative "cli/errors"
24
+ require_relative "cli/base"
25
+ require_relative "cli/commands/build"
26
+ require_relative "cli/commands/fmt"
27
+ require_relative "cli/commands/run"
28
+ require_relative "cli/commands/transpile"
29
+ require_relative "cli/commands/vet"
30
+ require_relative "cli/commands/version"
31
+
32
+ module Gob
33
+ class CLI < Thor
34
+ desc "build FILE", "Transpile and build a Gob program"
35
+ subcommand "build", Commands::Build
36
+ desc "fmt FILE...", "Format Gob source"
37
+ subcommand "fmt", Commands::Fmt
38
+ desc "run FILE [ARGS...]", "Transpile and run a Gob program"
39
+
40
+ def execute(*args)
41
+ command_args, command_options = Thor::Arguments.split(args)
42
+ invoke Commands::Run, command_args, command_options, invoked_via_subcommand: true
43
+ end
44
+
45
+ map "run" => "execute"
46
+ desc "transpile FILE", "Transpile Gob source to Go"
47
+ subcommand "transpile", Commands::Transpile
48
+ desc "vet FILE...", "Validate Gob source"
49
+ subcommand "vet", Commands::Vet
50
+ desc "version", "Print Gob version"
51
+ subcommand "version", Commands::Version
52
+ end
53
+ end