postscript 0.2.1 → 0.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bb4236c168f7e3c25c01c16280bb936e85efcd01178caa6fdfc4e0955d4ee699
4
- data.tar.gz: e9881343a17a7d760d8b8ae4af4b45b851db4a6c7475373a1ec132da43200d6a
3
+ metadata.gz: a67f3c33d3c8b28ef18f309cc96a1996eaddaac93adca74c0bd836ae82622009
4
+ data.tar.gz: 5fe476bdf68d584223d3ea3469fea6dd071a59caac0145be492a85710d30f32d
5
5
  SHA512:
6
- metadata.gz: 3c3e7e49e92abb85350de57e7c549743f38550e44238af5758a74ac5ea35ebb374f0fd221837467c385723ef7b8e85e502e7f9fba319ae4b1826694ec565d129
7
- data.tar.gz: 3cb1a87b4e8846f68a0d3b9c7a2c80c78261627dc3657ecede0436936debc40a908261b11a52961a602dc984e6cbcb1e412f8cf94787213524e3ddb5a09c9cc4
6
+ metadata.gz: e42c62b84db01140f828b81bbf69cc69c815850e1763b404b5873a12f40428307fcfb83efc63899166619f57ca6f36f579f0c906675296da89a4806107dbe8b0
7
+ data.tar.gz: 2ae13b78717bc72785797b3588c6c1ad559f55d7f85243ae3898a92f3ab87068b74ab455ba0ab4d626b5dbebc2d6a1b56e46f5c8c16f6a9a09fd771df6f024f9
data/.rubocop_todo.yml CHANGED
@@ -233,12 +233,6 @@ Performance/DeletePrefix:
233
233
  Exclude:
234
234
  - 'lib/postscript/source/ast_builder.rb'
235
235
 
236
- # Offense count: 2
237
- RSpec/BeforeAfterAll:
238
- Exclude:
239
- - 'spec/postscript/model/operator_spec.rb'
240
- - 'spec/postscript/model/operators_spec.rb'
241
-
242
236
  # Offense count: 28
243
237
  # This cop supports unsafe autocorrection (--autocorrect-all).
244
238
  # Configuration parameters: SkipBlocks, EnforcedStyle, OnlyStaticConstants.
data/Gemfile CHANGED
@@ -4,6 +4,17 @@ source "https://rubygems.org"
4
4
 
5
5
  gemspec
6
6
 
7
+ gem "json_schemer", group: :test
8
+
9
+ # Temporary pin like svg_conform#103; lens_core needs Ruby >= 3.1.
10
+ # TODO: Switch to the released gem and add it to the gemspec when published.
11
+ gem "lens_core", git: "https://github.com/claricle/lens_core.git",
12
+ ref: "98c21c82bc97e3c8c4038048acdcec4a177c5f7c",
13
+ install_if: -> { RUBY_VERSION >= "3.1" }
14
+
15
+ # Optional native engine (Source.parse(source, engine: :native)); not a
16
+ # dependency of the gem. parsanol needs Ruby >= 3.2.
17
+ gem "parsanol", ">= 1.3.96", install_if: -> { RUBY_VERSION >= "3.2" }
7
18
  gem "rake"
8
19
  gem "rspec"
9
20
  gem "rubocop"
data/README.adoc CHANGED
@@ -43,6 +43,207 @@ ps_text = Postscript::Serializer.call(program)
43
43
  puts ps_text
44
44
  ----
45
45
 
46
+ === Native engine (optional)
47
+
48
+ `engine: :native` tokenizes with the PostScript PARG grammar in
49
+ `grammars/` on the parsanol native engine:
50
+
51
+ [source,ruby]
52
+ ----
53
+ program = Postscript.parse(source, engine: :native)
54
+ Postscript::Source::NativeEngine.available? # => true when it can run
55
+ ----
56
+
57
+ It needs the `parsanol` gem (1.3.96 or newer, Ruby 3.2 or newer), which
58
+ this gem does not depend on: add it to your Gemfile to use it. Without
59
+ it, `engine: :native` raises `Postscript::NativeEngineUnavailable`.
60
+
61
+ It produces the same tokens, programs and errors as the default Ruby
62
+ lexer; where the grammar rejects the source, the Ruby lexer tokenizes
63
+ it. It is currently slower than the Ruby lexer, so the default is
64
+ `engine: :ruby`. `benchmark/parse.rb --engine native` measures both.
65
+
66
+ == Language levels
67
+
68
+ One grammar parses every language level; levels are vocabulary data,
69
+ not grammar. An operator's level comes from the PostScript Language
70
+ Guide's operator pages, plus `data/operator_overrides.yml` for what the
71
+ Guide lacks. See the full inventory in
72
+ link:docs/operators.adoc[docs/operators.adoc].
73
+
74
+ [source,ruby]
75
+ ----
76
+ program = Postscript.parse("0 0 moveto shfill")
77
+ Postscript.level_violations(program, level: 2)
78
+ # => [#<struct Postscript::LevelViolation keyword="shfill", level=3>]
79
+ ----
80
+
81
+ Without `level:`, the program's `%%LanguageLevel` header is used. With
82
+ no header, or one outside 1..3, the level is 3. An explicit `level:` must
83
+ be 1, 2 or 3, or it raises `ArgumentError`. With a valid level, it reports
84
+ violations and never raises for a parsed program.
85
+
86
+ Names with no vocabulary entry and calls to user procedures are never
87
+ violations.
88
+
89
+ Known limitation: operators inside `<< >>` dictionaries, and operators
90
+ inside `[ ]` that the AST builder drops, are not checked.
91
+
92
+ == Validation
93
+
94
+ `Postscript.validate` returns a `LensCore::Report` using the LensCore
95
+ diagnostics v1 contract. Validation requires `lens_core` and Ruby >= 3.1;
96
+ add the dependency to your Gemfile (currently pinned to commit
97
+ `98c21c82bc97e3c8c4038048acdcec4a177c5f7c` of
98
+ https://github.com/claricle/lens_core[lens_core]). It loads lazily, so parsing
99
+ and serialization remain usable without it. If it cannot load, validation
100
+ raises `Postscript::Error` with the dependency requirements.
101
+
102
+ [source,ruby]
103
+ ----
104
+ report = Postscript.validate("0 0 moveto shfill", level: 1)
105
+ report.valid # => false
106
+ report.summary # => { "errors" => 1, "warnings" => 0, "infos" => 0 }
107
+ diagnostic = report.diagnostics.first
108
+ diagnostic.requirement_id # => "postscript.level.operator-available"
109
+ diagnostic.message # => "shfill is Level 3; this program targets Level 1"
110
+ diagnostic.context
111
+ # => { "keyword" => "shfill", "required_level" => 3, "target_level" => 1 }
112
+ diagnostic.remediation # => "declare %%LanguageLevel: 3 or remove shfill"
113
+ ----
114
+
115
+ Grammar errors stop validation before model checks. Level findings cite
116
+ the operator's vocabulary source and use the same explicit-level, header
117
+ and default rules as `Postscript.level_violations`. For invalid grammar,
118
+ the profile uses the explicit level or defaults to Level 3. Unknown names
119
+ produce warnings; warnings alone do not make `valid` false. This static
120
+ check recognizes `def` bindings but does not simulate execution or
121
+ dictionary scope, and shares the model limitations described above.
122
+
123
+ The default `engine: :auto` uses the native engine when available and Ruby
124
+ otherwise. Pass `engine: :ruby` to select Ruby, or `engine: :native` to
125
+ require native support (`Postscript::NativeEngineUnavailable` is raised
126
+ if unavailable). The defaults of `parse` and `tokenize` remain `:ruby`.
127
+
128
+ Grammar findings retain each engine's message and put `line`, `column`,
129
+ `offset`, `rule`, `expected` and `engine` in `context`. Coordinates are
130
+ passed through from the engine. Diagnostics never have a `location`:
131
+ LensCore v1 has no text location type. Model findings have no `engine`
132
+ field, so accepted programs give identical reports across engines.
133
+ Requirement IDs identify rules, with operator names kept in context.
134
+ Diagnostic text is decoded as UTF-8; invalid byte sequences are displayed
135
+ with the replacement character (`�`). The source bytes are unchanged.
136
+
137
+ Reports omit timestamps unless the caller supplies `timestamp:` (a `Time`
138
+ or RFC 3339 string). All three LensCore renderers accept the same report:
139
+
140
+ [source,ruby]
141
+ ----
142
+ report = Postscript.validate("{ unclosed", level: 1, engine: :ruby)
143
+ puts LensCore::Renderers::Json.render(report)
144
+ puts LensCore::Renderers::Yaml.render(report)
145
+ puts LensCore::Renderers::Human.render(report)
146
+ ----
147
+
148
+ Json output:
149
+
150
+ [source,json]
151
+ ----
152
+ {
153
+ "schema_version": 1,
154
+ "producer": {
155
+ "name": "postscript",
156
+ "version": "0.2.1"
157
+ },
158
+ "profile": {
159
+ "id": "postscript-level-1",
160
+ "citations": [
161
+ {
162
+ "uri": "https://www.adobe.com/products/postscript/pdfs/PLRM.pdf",
163
+ "label": "PLRM 3rd ed.: LanguageLevel 1"
164
+ }
165
+ ]
166
+ },
167
+ "valid": false,
168
+ "summary": {
169
+ "errors": 1,
170
+ "warnings": 0,
171
+ "infos": 0
172
+ },
173
+ "diagnostics": [
174
+ {
175
+ "severity": "error",
176
+ "kind": "grammar",
177
+ "requirement_id": "postscript.grammar.syntax",
178
+ "message": "unbalanced procedure braces",
179
+ "citations": [
180
+ {
181
+ "uri": "https://www.adobe.com/products/postscript/pdfs/PLRM.pdf",
182
+ "label": "PLRM 3rd ed.: LanguageLevel 1"
183
+ }
184
+ ],
185
+ "context": {
186
+ "column": 1,
187
+ "engine": "ruby",
188
+ "expected": [],
189
+ "line": 1,
190
+ "offset": 0,
191
+ "rule": null
192
+ }
193
+ }
194
+ ]
195
+ }
196
+ ----
197
+
198
+ Yaml output:
199
+
200
+ [source,yaml]
201
+ ----
202
+ ---
203
+ schema_version: 1
204
+ producer:
205
+ name: postscript
206
+ version: 0.2.1
207
+ profile:
208
+ id: postscript-level-1
209
+ citations:
210
+ - uri: https://www.adobe.com/products/postscript/pdfs/PLRM.pdf
211
+ label: 'PLRM 3rd ed.: LanguageLevel 1'
212
+ valid: false
213
+ summary:
214
+ errors: 1
215
+ warnings: 0
216
+ infos: 0
217
+ diagnostics:
218
+ - severity: error
219
+ kind: grammar
220
+ requirement_id: postscript.grammar.syntax
221
+ message: unbalanced procedure braces
222
+ citations:
223
+ - uri: https://www.adobe.com/products/postscript/pdfs/PLRM.pdf
224
+ label: 'PLRM 3rd ed.: LanguageLevel 1'
225
+ context:
226
+ column: 1
227
+ engine: ruby
228
+ expected: []
229
+ line: 1
230
+ offset: 0
231
+ rule:
232
+ ----
233
+
234
+ Human output:
235
+
236
+ [source,text]
237
+ ----
238
+ postscript 0.2.1, profile postscript-level-1
239
+ INVALID: 1 error, 0 warnings, 0 infos
240
+
241
+ error postscript.grammar.syntax (grammar)
242
+ unbalanced procedure braces
243
+ see: PLRM 3rd ed.: LanguageLevel 1 https://www.adobe.com/products/postscript/pdfs/PLRM.pdf
244
+ context: {"column":1,"engine":"ruby","expected":[],"line":1,"offset":0,"rule":null}
245
+ ----
246
+
46
247
  == Architecture
47
248
 
48
249
  Three layers, each with a single responsibility: