foxtail-tools 0.5.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 (50) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +19 -0
  3. data/LICENSE.txt +21 -0
  4. data/README.md +66 -0
  5. data/exe/foxtail +12 -0
  6. data/lib/foxtail/cli/commands/check.rb +60 -0
  7. data/lib/foxtail/cli/commands/dump.rb +43 -0
  8. data/lib/foxtail/cli/commands/ids.rb +73 -0
  9. data/lib/foxtail/cli/commands/tidy.rb +107 -0
  10. data/lib/foxtail/cli.rb +59 -0
  11. data/lib/foxtail/syntax/error.rb +8 -0
  12. data/lib/foxtail/syntax/parser/ast/annotation.rb +23 -0
  13. data/lib/foxtail/syntax/parser/ast/attribute.rb +23 -0
  14. data/lib/foxtail/syntax/parser/ast/base_comment.rb +19 -0
  15. data/lib/foxtail/syntax/parser/ast/base_literal.rb +24 -0
  16. data/lib/foxtail/syntax/parser/ast/base_node.rb +89 -0
  17. data/lib/foxtail/syntax/parser/ast/call_arguments.rb +23 -0
  18. data/lib/foxtail/syntax/parser/ast/comment.rb +13 -0
  19. data/lib/foxtail/syntax/parser/ast/function_reference.rb +23 -0
  20. data/lib/foxtail/syntax/parser/ast/group_comment.rb +13 -0
  21. data/lib/foxtail/syntax/parser/ast/identifier.rb +19 -0
  22. data/lib/foxtail/syntax/parser/ast/junk.rb +23 -0
  23. data/lib/foxtail/syntax/parser/ast/message.rb +28 -0
  24. data/lib/foxtail/syntax/parser/ast/message_reference.rb +23 -0
  25. data/lib/foxtail/syntax/parser/ast/named_argument.rb +23 -0
  26. data/lib/foxtail/syntax/parser/ast/number_literal.rb +24 -0
  27. data/lib/foxtail/syntax/parser/ast/pattern.rb +22 -0
  28. data/lib/foxtail/syntax/parser/ast/placeable.rb +21 -0
  29. data/lib/foxtail/syntax/parser/ast/resource.rb +55 -0
  30. data/lib/foxtail/syntax/parser/ast/resource_comment.rb +13 -0
  31. data/lib/foxtail/syntax/parser/ast/select_expression.rb +23 -0
  32. data/lib/foxtail/syntax/parser/ast/span.rb +22 -0
  33. data/lib/foxtail/syntax/parser/ast/string_literal.rb +45 -0
  34. data/lib/foxtail/syntax/parser/ast/syntax_node.rb +22 -0
  35. data/lib/foxtail/syntax/parser/ast/term.rb +28 -0
  36. data/lib/foxtail/syntax/parser/ast/term_reference.rb +25 -0
  37. data/lib/foxtail/syntax/parser/ast/text_element.rb +19 -0
  38. data/lib/foxtail/syntax/parser/ast/variable_reference.rb +21 -0
  39. data/lib/foxtail/syntax/parser/ast/variant.rb +25 -0
  40. data/lib/foxtail/syntax/parser/ast.rb +12 -0
  41. data/lib/foxtail/syntax/parser/parse_error.rb +94 -0
  42. data/lib/foxtail/syntax/parser/stream.rb +338 -0
  43. data/lib/foxtail/syntax/parser.rb +797 -0
  44. data/lib/foxtail/syntax/serializer.rb +242 -0
  45. data/lib/foxtail/syntax/visitor.rb +61 -0
  46. data/lib/foxtail/syntax.rb +12 -0
  47. data/lib/foxtail/tools/error.rb +8 -0
  48. data/lib/foxtail/tools/version.rb +9 -0
  49. data/lib/foxtail-tools.rb +22 -0
  50. metadata +141 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6d238709640d88ba5666a1047d51bac44bf9153ff78bf82c12acfa32ea432bfb
4
+ data.tar.gz: 35acfed51d9030564151109c61e2d8641e6152c16e5acb169eb3f3eb5894d194
5
+ SHA512:
6
+ metadata.gz: 0cde2d406e429b21a9684b6c5c68d1061dddb63734ac13de3cc21bea755d8cfbb9fbaf98581c3bfcc4106a72e05b452fc85a44c557ac69d04fca854509e3b022
7
+ data.tar.gz: 787d507da2c27350b4badf12fa8eabbd75f3e4d626fe99028a234a2bf387b59aa9c8653bff789f66c22d52cc6a853b65ca7f9014b89f8305159da05122ff2ecf
data/CHANGELOG.md ADDED
@@ -0,0 +1,19 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.5.0] - 2026-05-08
4
+
5
+ ### Added
6
+ - **Syntax Tooling**
7
+ - FTL syntax parser with AST support
8
+ - Serializer for round-trip formatting
9
+ - **CLI Commands**
10
+ - `foxtail check` - Check FTL files for syntax errors
11
+ - `foxtail dump` - Dump FTL files as AST in JSON format
12
+ - `foxtail ids` - Extract message and term IDs from FTL files
13
+ - `foxtail tidy` - Format FTL files with consistent style
14
+ - **Examples**
15
+ - AST transformation example in `examples/01_prefix_message_ids.rb`
16
+
17
+ ### Compatibility
18
+ - fluent.js syntax parser: 97/98 (99.0%)
19
+ - Ruby: 3.3+ supported
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 OZAWA Sakuro
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,66 @@
1
+ # 🦊 Foxtail Tools 🌐
2
+
3
+ Tooling for authoring and validating [Project Fluent](https://projectfluent.org/) files in Ruby.
4
+
5
+ ## Features
6
+
7
+ - Full syntax parser with spans and comments
8
+ - Serializer for round-trip formatting
9
+ - CLI tools: check, dump, ids, tidy
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem "foxtail-tools"
17
+ ```
18
+
19
+ Then execute:
20
+
21
+ ```bash
22
+ $ bundle install
23
+ ```
24
+
25
+ Or install it yourself as:
26
+
27
+ ```bash
28
+ $ gem install foxtail-tools
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ ### CLI
34
+
35
+ ```bash
36
+ foxtail check messages.ftl
37
+ foxtail dump messages.ftl
38
+ foxtail ids messages.ftl
39
+ foxtail tidy messages.ftl
40
+ ```
41
+
42
+ See [doc/cli.md](doc/cli.md) for the full CLI reference.
43
+
44
+ ### Syntax API
45
+
46
+ ```ruby
47
+ require "foxtail-tools"
48
+
49
+ parser = Foxtail::Syntax::Parser.new
50
+ ast = parser.parse("hello = Hello")
51
+ ```
52
+
53
+ ## Compatibility
54
+
55
+ The syntax parser passes 97 of 98 [fluent-syntax](https://github.com/projectfluent/fluent.js/tree/main/fluent-syntax) test fixtures (99.0%).
56
+
57
+ The `leading_dots` fixture is a known mismatch — this test also fails in fluent.js itself.
58
+
59
+ ## Documentation
60
+
61
+ - [CLI Reference](doc/cli.md)
62
+ - [Architecture](doc/architecture.md)
63
+
64
+ ## License
65
+
66
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/exe/foxtail ADDED
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "dry/cli"
5
+ require "foxtail-tools"
6
+
7
+ begin
8
+ Dry::CLI.new(Foxtail::CLI).call
9
+ rescue Foxtail::CLI::Error => e
10
+ warn e.message
11
+ exit 1
12
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ # Command-line interface for Foxtail
5
+ module CLI
6
+ module Commands
7
+ # Check FTL files for syntax errors
8
+ class Check < Dry::CLI::Command
9
+ desc "Check FTL files for syntax errors"
10
+
11
+ argument :files, type: :array, required: true, desc: "FTL files to check"
12
+
13
+ option :quiet, type: :flag, default: false, aliases: ["-q"], desc: "Only show errors, no summary"
14
+
15
+ # Execute the check command
16
+ # @param files [Array<String>] FTL files to check
17
+ # @param quiet [Boolean] Only show errors, no summary
18
+ # @return [void]
19
+ def call(files:, quiet:, **)
20
+ raise Foxtail::CLI::NoFilesError if files.empty?
21
+
22
+ total_errors = 0
23
+ total_files = 0
24
+
25
+ files.each do |file|
26
+ errors = check_file(file)
27
+ total_files += 1
28
+ total_errors += errors.size
29
+
30
+ errors.each do |error|
31
+ err.puts error
32
+ end
33
+ end
34
+
35
+ unless quiet
36
+ out.puts
37
+ out.puts "#{total_files} file(s) checked, #{total_errors} error(s) found"
38
+ end
39
+
40
+ raise Foxtail::CLI::CheckError, total_errors if total_errors > 0
41
+ end
42
+
43
+ private def check_file(path)
44
+ content = File.read(path)
45
+ parser = Foxtail::Syntax::Parser.new
46
+ resource = parser.parse(content)
47
+
48
+ resource.body.filter_map {|entry|
49
+ format_junk_error(path, entry) if entry.is_a?(Foxtail::Syntax::Parser::AST::Junk)
50
+ }
51
+ end
52
+
53
+ private def format_junk_error(path, junk)
54
+ first_line = junk.content.lines.first&.chomp || ""
55
+ "#{path}: syntax error: #{first_line}"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Foxtail
6
+ # Command-line interface for Foxtail
7
+ module CLI
8
+ module Commands
9
+ # Dump FTL files as AST in JSON format
10
+ class Dump < Dry::CLI::Command
11
+ desc "Dump FTL files as AST in JSON format"
12
+
13
+ argument :files, type: :array, required: true, desc: "FTL files to dump"
14
+
15
+ option :with_spans, type: :flag, default: false, desc: "Include span information in output"
16
+
17
+ # Execute the dump command
18
+ # @param files [Array<String>] FTL files to dump
19
+ # @param with_spans [Boolean] Include span information
20
+ # @return [void]
21
+ def call(files:, with_spans:, **)
22
+ raise Foxtail::CLI::NoFilesError if files.empty?
23
+
24
+ results = files.map {|file| dump_file(Pathname(file), with_spans:) }
25
+
26
+ output = files.size == 1 ? results.first : results
27
+ out.puts JSON.pretty_generate(output)
28
+ end
29
+
30
+ private def dump_file(path, with_spans:)
31
+ content = path.read
32
+ parser = Foxtail::Syntax::Parser.new(with_spans:)
33
+ resource = parser.parse(content)
34
+
35
+ {
36
+ "file" => path.to_s,
37
+ "ast" => resource.to_h
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,73 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module Foxtail
6
+ # Command-line interface for Foxtail
7
+ module CLI
8
+ module Commands
9
+ # Extract message and term IDs from FTL files
10
+ class Ids < Dry::CLI::Command
11
+ desc "Extract message and term IDs from FTL files"
12
+
13
+ argument :files, type: :array, required: true, desc: "FTL files to extract IDs from"
14
+
15
+ option :only_messages, type: :flag, default: false, aliases: ["-m"], desc: "Show only message IDs"
16
+ option :only_terms, type: :flag, default: false, aliases: ["-t"], desc: "Show only term IDs"
17
+ option :with_attributes, type: :flag, default: false, aliases: ["-a"], desc: "Include attribute names"
18
+ option :json, type: :flag, default: false, aliases: ["-j"], desc: "Output as JSON array"
19
+
20
+ # Execute the ids command
21
+ # @param files [Array<String>] FTL files to extract IDs from
22
+ # @param only_messages [Boolean] Show only message IDs
23
+ # @param only_terms [Boolean] Show only term IDs
24
+ # @param with_attributes [Boolean] Include attribute names
25
+ # @param json [Boolean] Output as JSON array
26
+ # @return [void]
27
+ def call(files:, only_messages:, only_terms:, with_attributes:, json:, **)
28
+ raise Foxtail::CLI::NoFilesError if files.empty?
29
+
30
+ ids = []
31
+
32
+ files.each do |file|
33
+ ids.concat(extract_ids(file, only_messages:, only_terms:, with_attributes:))
34
+ end
35
+
36
+ if json
37
+ out.puts JSON.pretty_generate(ids)
38
+ else
39
+ ids.each {|id| out.puts id }
40
+ end
41
+ end
42
+
43
+ private def extract_ids(path, only_messages:, only_terms:, with_attributes:)
44
+ ids = []
45
+ content = File.read(path)
46
+ parser = Foxtail::Syntax::Parser.new
47
+ resource = parser.parse(content)
48
+
49
+ resource.each_entry do |entry|
50
+ case entry
51
+ when Foxtail::Syntax::Parser::AST::Message
52
+ next if only_terms
53
+
54
+ ids << entry.id.name
55
+ ids.concat(attribute_ids(entry)) if with_attributes
56
+ when Foxtail::Syntax::Parser::AST::Term
57
+ next if only_messages
58
+
59
+ ids << "-#{entry.id.name}"
60
+ ids.concat(attribute_ids(entry, prefix: "-")) if with_attributes
61
+ end
62
+ end
63
+
64
+ ids
65
+ end
66
+
67
+ private def attribute_ids(entry, prefix: "")
68
+ entry.attributes.map {|attr| "#{prefix}#{entry.id.name}.#{attr.id.name}" }
69
+ end
70
+ end
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ # Command-line interface for Foxtail
5
+ module CLI
6
+ module Commands
7
+ # Format FTL files with consistent style
8
+ class Tidy < Dry::CLI::Command
9
+ desc "Format FTL files with consistent style"
10
+
11
+ argument :files, type: :array, required: true, desc: "FTL files to format"
12
+
13
+ option :write, type: :flag, default: false, aliases: ["-w"], desc: "Write result back to source file"
14
+ option :check, type: :flag, default: false, aliases: ["-c"], desc: "Check if files are formatted (for CI)"
15
+ option :diff, type: :flag, default: false, aliases: ["-d"], desc: "Show diff instead of formatted output"
16
+ option :with_junk, type: :flag, default: false, desc: "Allow formatting files with syntax errors"
17
+
18
+ # Execute the tidy command
19
+ # @param files [Array<String>] FTL files to format
20
+ # @param write [Boolean] Write result back to source file
21
+ # @param check [Boolean] Check if files are formatted (for CI)
22
+ # @param diff [Boolean] Show diff instead of formatted output
23
+ # @param with_junk [Boolean] Allow formatting files with syntax errors
24
+ # @return [void]
25
+ def call(files:, write:, check:, diff:, with_junk:, **)
26
+ raise Foxtail::CLI::NoFilesError if files.empty?
27
+
28
+ files_with_errors = []
29
+ files_needing_format = []
30
+ multiple_files = files.size > 1
31
+
32
+ files.each do |file|
33
+ result = process_file(file, write:, check:, diff:, with_junk:, multiple_files:)
34
+ case result
35
+ when :has_errors
36
+ files_with_errors << file
37
+ when :needs_format
38
+ files_needing_format << file
39
+ end
40
+ end
41
+
42
+ raise Foxtail::CLI::TidyError, files_with_errors unless files_with_errors.empty?
43
+ raise Foxtail::CLI::TidyCheckError, files_needing_format if check && !files_needing_format.empty?
44
+ end
45
+
46
+ private def process_file(path, write:, check:, diff:, with_junk:, multiple_files:)
47
+ content = File.read(path)
48
+ parser = Foxtail::Syntax::Parser.new
49
+ resource = parser.parse(content)
50
+
51
+ # Check for Junk entries (syntax errors)
52
+ has_junk = resource.body.any?(Foxtail::Syntax::Parser::AST::Junk)
53
+ return :has_errors if has_junk && !with_junk
54
+
55
+ serializer = Foxtail::Syntax::Serializer.new(with_junk:)
56
+ formatted = serializer.serialize(resource)
57
+
58
+ if check
59
+ return :needs_format if content != formatted
60
+
61
+ return :ok
62
+ end
63
+
64
+ if diff
65
+ output_diff(path, content, formatted)
66
+ return :ok
67
+ end
68
+
69
+ if write
70
+ File.write(path, formatted) if content != formatted
71
+ return :ok
72
+ end
73
+
74
+ # Default: output to stdout
75
+ output_formatted(path, formatted, multiple_files)
76
+ :ok
77
+ end
78
+
79
+ private def output_formatted(path, formatted, multiple_files)
80
+ if multiple_files
81
+ out.puts "==> #{path} <=="
82
+ out.puts formatted
83
+ else
84
+ out.print formatted
85
+ end
86
+ end
87
+
88
+ private def output_diff(path, original, formatted)
89
+ return if original == formatted
90
+
91
+ require "tempfile"
92
+
93
+ Tempfile.create(["original", ".ftl"]) do |orig_file|
94
+ Tempfile.create(["formatted", ".ftl"]) do |fmt_file|
95
+ orig_file.write(original)
96
+ orig_file.flush
97
+ fmt_file.write(formatted)
98
+ fmt_file.flush
99
+
100
+ out.print IO.popen(["diff", "-u", "--label", path, orig_file.path, "--label", "#{path} (formatted)", fmt_file.path], &:read)
101
+ end
102
+ end
103
+ end
104
+ end
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/cli"
4
+
5
+ module Foxtail
6
+ # Command-line interface for Foxtail
7
+ module CLI
8
+ extend Dry::CLI::Registry
9
+
10
+ # Base error class for CLI-specific exceptions
11
+ class Error < Foxtail::Tools::Error; end
12
+
13
+ # Raised when no files are specified for a CLI command
14
+ class NoFilesError < Error
15
+ def initialize = super("No files specified")
16
+ end
17
+
18
+ # Raised when check command finds errors in FTL files
19
+ class CheckError < Error
20
+ # @return [Integer] Number of errors found
21
+ attr_reader :error_count
22
+
23
+ # @param error_count [Integer] Number of errors found
24
+ def initialize(error_count)
25
+ @error_count = error_count
26
+ super("Check found #{error_count} error(s)")
27
+ end
28
+ end
29
+
30
+ # Raised when tidy command encounters syntax errors without --with-junk
31
+ class TidyError < Error
32
+ # @return [Array<String>] Paths to files with syntax errors
33
+ attr_reader :files_with_errors
34
+
35
+ # @param files_with_errors [Array<String>] Paths to files with syntax errors
36
+ def initialize(files_with_errors)
37
+ @files_with_errors = files_with_errors
38
+ super("Files contain syntax errors: #{files_with_errors.join(", ")}")
39
+ end
40
+ end
41
+
42
+ # Raised when tidy --check finds files that need formatting
43
+ class TidyCheckError < Error
44
+ # @return [Array<String>] Paths to files needing formatting
45
+ attr_reader :files_needing_format
46
+
47
+ # @param files_needing_format [Array<String>] Paths to files needing formatting
48
+ def initialize(files_needing_format)
49
+ @files_needing_format = files_needing_format
50
+ super("Files need formatting: #{files_needing_format.join(", ")}")
51
+ end
52
+ end
53
+
54
+ register "check", Commands::Check
55
+ register "dump", Commands::Dump
56
+ register "ids", Commands::Ids
57
+ register "tidy", Commands::Tidy
58
+ end
59
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ # Base error class for syntax-specific exceptions
6
+ class Error < Foxtail::Tools::Error; end
7
+ end
8
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ class Parser
6
+ module AST
7
+ # Represents parser error annotations with code, arguments, and message
8
+ class Annotation < SyntaxNode
9
+ attr_accessor :code
10
+ attr_accessor :arguments
11
+ attr_accessor :message
12
+
13
+ def initialize(code, arguments=[], message="")
14
+ super()
15
+ @code = code
16
+ @arguments = arguments
17
+ @message = message
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ class Parser
6
+ module AST
7
+ # Represents attributes of messages and terms (e.g., .attr = value)
8
+ class Attribute < SyntaxNode
9
+ attr_accessor :id
10
+ attr_accessor :value
11
+
12
+ def initialize(id, value)
13
+ super()
14
+ @id = id
15
+ @value = value
16
+ end
17
+
18
+ def children = [id, value]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ class Parser
6
+ module AST
7
+ # Base class for all comment types in Fluent syntax
8
+ class BaseComment < SyntaxNode
9
+ attr_accessor :content
10
+
11
+ def initialize(content)
12
+ super()
13
+ @content = content
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ class Parser
6
+ module AST
7
+ # Base class for literal values (strings and numbers) with parsing capability
8
+ class BaseLiteral < SyntaxNode
9
+ attr_accessor :value
10
+
11
+ def initialize(value)
12
+ super()
13
+ # The "value" field contains the exact contents of the literal,
14
+ # character-for-character.
15
+ @value = value
16
+ end
17
+
18
+ # Abstract method - subclasses must implement
19
+ def parse = raise NotImplementedError, "Subclasses must implement parse method"
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/inflector"
4
+
5
+ module Foxtail
6
+ module Syntax
7
+ class Parser
8
+ module AST
9
+ # Base class for all AST nodes in the Fluent parser
10
+ # Provides common functionality for type management and node equality
11
+ class BaseNode
12
+ INFLECTOR = Dry::Inflector.new.freeze
13
+ private_constant :INFLECTOR
14
+
15
+ attr_accessor :type
16
+
17
+ def initialize = @type = self.class.name.split("::").last
18
+
19
+ # Compare nodes for equality, ignoring span information
20
+ def ==(other)
21
+ return false unless other.is_a?(BaseNode)
22
+
23
+ this_keys = instance_variables.to_set {|v| v.to_s.delete("@") }
24
+ other_keys = other.instance_variables.to_set {|v| v.to_s.delete("@") }
25
+
26
+ # Always ignore span information in equality comparison
27
+ this_keys.delete("span")
28
+ other_keys.delete("span")
29
+
30
+ return false if this_keys.size != other_keys.size
31
+
32
+ this_keys.each do |field_name|
33
+ return false unless other_keys.include?(field_name)
34
+
35
+ this_val = instance_variable_get("@#{field_name}")
36
+ other_val = other.instance_variable_get("@#{field_name}")
37
+
38
+ return false unless this_val.class == other_val.class
39
+
40
+ if this_val.is_a?(Array) && other_val.is_a?(Array)
41
+ return false if this_val.length != other_val.length
42
+
43
+ this_val.each_with_index do |item, i|
44
+ return false unless item == other_val[i]
45
+ end
46
+ elsif this_val != other_val
47
+ return false
48
+ end
49
+ end
50
+
51
+ true
52
+ end
53
+
54
+ # Accept a visitor and dispatch to the appropriate visit method
55
+ def accept(visitor)
56
+ method_name = "visit_#{INFLECTOR.underscore(self.class.name.split("::").last)}"
57
+ visitor.public_send(method_name, self)
58
+ end
59
+
60
+ # Returns an array of child nodes for traversal
61
+ # Subclasses with children should override this method
62
+ def children = []
63
+
64
+ # Convert node to hash representation for JSON serialization
65
+ def to_h
66
+ result = {}
67
+ instance_variables.each do |var|
68
+ key = var.to_s.delete("@")
69
+ value = instance_variable_get(var)
70
+ result[key] = serialize_value(value)
71
+ end
72
+ result
73
+ end
74
+
75
+ private def serialize_value(value)
76
+ case value
77
+ when BaseNode
78
+ value.to_h
79
+ when Array
80
+ value.map {|v| serialize_value(v) }
81
+ else
82
+ value
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Foxtail
4
+ module Syntax
5
+ class Parser
6
+ module AST
7
+ # Represents function call arguments (both positional and named)
8
+ class CallArguments < SyntaxNode
9
+ attr_accessor :positional
10
+ attr_accessor :named
11
+
12
+ def initialize(positional=[], named=[])
13
+ super()
14
+ @positional = positional
15
+ @named = named
16
+ end
17
+
18
+ def children = [*positional, *named]
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end