parsanol 1.0.1-aarch64-linux → 1.0.2-aarch64-linux

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.
@@ -1,107 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- # Parsanol::RubyTransform - Ruby Transform Mode (Parslet-Compatible)
4
- #
5
- # This is the default parsing mode that provides maximum flexibility.
6
- # - Parsing can use Rust (if available) or pure Ruby
7
- # - Transformation happens in Ruby using Parslet-style Transform class
8
- #
9
- # Usage:
10
- # class MyParser < Parsanol::Parser
11
- # include Parsanol::RubyTransform
12
- # rule(:number) { match('[0-9]').repeat(1).as(:int) }
13
- # root(:number)
14
- # end
15
- #
16
- # parser = MyParser.new
17
- # tree = parser.parse("42") # Returns generic tree
18
- # ast = transform.apply(tree) # Transform in Ruby
19
- #
20
- # To use Rust backend for parsing:
21
- # class MyParser < Parsanol::Parser
22
- # include Parsanol::RubyTransform
23
- # parse_backend :rust # Will raise if native extension not available
24
- # ...
25
- # end
26
-
27
- module Parsanol
28
- module RubyTransform
29
- def self.included(base)
30
- base.extend(ClassMethods)
31
- end
32
-
33
- module ClassMethods
34
- # Get or set the parsing backend
35
- # @param backend [Symbol] :ruby (default) or :rust
36
- # @return [Symbol] Current backend setting
37
- def parse_backend(backend = nil)
38
- @parse_backend = backend if backend
39
- @parse_backend ||= :ruby
40
- end
41
-
42
- # Setter for parsing backend
43
- # @param backend [Symbol] :ruby or :rust
44
- def parse_backend=(backend)
45
- @parse_backend = backend
46
- end
47
-
48
- # Configure parsing to use Rust backend
49
- # Raises LoadError if native extension not available
50
- def use_rust_backend!
51
- unless Parsanol::Native.available?
52
- raise LoadError,
53
- "Rust backend requested but native extension not available. " \
54
- "Run `rake compile` to build the extension."
55
- end
56
- @parse_backend = :rust
57
- end
58
-
59
- # Configure parsing to use pure Ruby (default)
60
- def use_ruby_backend!
61
- @parse_backend = :ruby
62
- end
63
- end
64
-
65
- # Parse input and return generic tree
66
- #
67
- # @param input [String] The input string to parse
68
- # @param options [Hash] Parse options
69
- # @option options [Boolean] :consume_all (true) Consume entire input
70
- # @return [Hash, Array, String, Parsanol::Slice] Parse tree
71
- # @raise [Parsanol::ParseFailed] If parsing fails
72
- def parse(input, options = {})
73
- if self.class.parse_backend == :rust && Parsanol::Native.available?
74
- parse_with_rust(input, options)
75
- else
76
- parse_with_ruby(input, options)
77
- end
78
- end
79
-
80
- # Parse and apply transform in one step
81
- #
82
- # @param input [String] The input string to parse
83
- # @param transform [Parsanol::Transform] Transform to apply
84
- # @param options [Hash] Parse options
85
- # @return [Object] Transformed result
86
- def parse_with_transform(input, transform, options = {})
87
- tree = parse(input, options)
88
- transform.apply(tree)
89
- end
90
-
91
- private
92
-
93
- # Parse using Rust native extension
94
- def parse_with_rust(input, options = {})
95
- options.fetch(:consume_all, true)
96
-
97
- # Use native parser with Parslet-compatible output
98
- Parsanol::Native.parse_parslet_compatible(root, input)
99
- end
100
-
101
- # Parse using pure Ruby
102
- def parse_with_ruby(input, options = {})
103
- # Call the root parslet's parse method directly
104
- root.parse(input, options)
105
- end
106
- end
107
- end
@@ -1,94 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require 'json'
4
-
5
- # Parsanol::Serialized - Serialized Transform Mode (JSON Output)
6
- #
7
- # This mode provides cross-language compatibility through JSON serialization.
8
- # - Parsing AND transformation happen in Rust for maximum performance
9
- # - Output is a JSON string that can be deserialized to any format
10
- # - REQUIRES native extension (will raise LoadError if not available)
11
- #
12
- # Usage:
13
- # class MyParser < Parsanol::Parser
14
- # include Parsanol::Serialized
15
- # rule(:number) { match('[0-9]').repeat(1).as(:int) }
16
- # root(:number)
17
- # end
18
- #
19
- # parser = MyParser.new
20
- # json = parser.parse_to_json("42") # Returns JSON string
21
- # # => '{"int": "42"}'
22
- #
23
- # # With a deserializer class
24
- # result = parser.parse_to_struct("42", MyDeserializer)
25
- #
26
- # Performance: Faster than RubyTransform because transform happens in Rust.
27
- # Memory: Higher than ZeroCopy due to JSON serialization overhead.
28
-
29
- module Parsanol
30
- module Serialized
31
- def self.included(base)
32
- base.extend(ClassMethods)
33
- end
34
-
35
- module ClassMethods
36
- # Define output schema for transformation
37
- # This is optional but helps with type checking
38
- #
39
- # @param schema [Hash] Schema definition
40
- # @example
41
- # output_schema(
42
- # number: { type: :integer },
43
- # binop: { type: :object, properties: [:left, :op, :right] }
44
- # )
45
- def output_schema(schema = nil)
46
- @output_schema = schema if schema
47
- @output_schema ||= {}
48
- end
49
- end
50
-
51
- # Parse input and return JSON string
52
- #
53
- # @param input [String] The input string to parse
54
- # @return [String] JSON string representing the parse result
55
- # @raise [LoadError] If native extension not available
56
- # @raise [Parsanol::ParseFailed] If parsing fails
57
- def parse_to_json(input)
58
- unless Parsanol::Native.available?
59
- raise LoadError,
60
- "Serialized mode requires native extension for JSON serialization. " \
61
- "Run `rake compile` or use Parsanol::RubyTransform for Ruby-only parsing."
62
- end
63
-
64
- grammar_json = Parsanol::Native.serialize_grammar(root)
65
- Parsanol::Native.parse_to_json(grammar_json, input)
66
- end
67
-
68
- # Parse input and deserialize to a Ruby object
69
- #
70
- # @param input [String] The input string to parse
71
- # @param deserializer_class [Class] Class with .from_json method
72
- # @return [Object] Deserialized object
73
- # @raise [LoadError] If native extension not available
74
- # @raise [Parsanol::ParseFailed] If parsing fails
75
- def parse_to_struct(input, deserializer_class)
76
- json = parse_to_json(input)
77
- deserializer_class.from_json(json)
78
- end
79
-
80
- # Parse input and return Ruby Hash (parsed JSON)
81
- #
82
- # @param input [String] The input string to parse
83
- # @return [Hash, Array] Ruby object from JSON
84
- # @raise [LoadError] If native extension not available
85
- # @raise [Parsanol::ParseFailed] If parsing fails
86
- def parse(input, _options = {})
87
- json = parse_to_json(input)
88
- JSON.parse(json)
89
- end
90
-
91
- # Alias for consistency with other modes
92
- alias parse_to_hash parse
93
- end
94
- end