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 +4 -4
- data/.rubocop_todo.yml +0 -6
- data/Gemfile +11 -0
- data/README.adoc +201 -0
- data/data/guide_operators.yml +667 -0
- data/data/operator_overrides.yml +163 -0
- data/docs/operators.adoc +205 -0
- data/docs/plans/issue-4-native-parser.md +162 -0
- data/grammars/README.md +22 -0
- data/grammars/postscript.json +1 -0
- data/grammars/postscript.parg +161 -0
- data/grammars/upstream.yml +7 -0
- data/lib/postscript/cli.rb +12 -8
- data/lib/postscript/diagnostic.rb +14 -0
- data/lib/postscript/errors.rb +4 -0
- data/lib/postscript/lens_citations.rb +64 -0
- data/lib/postscript/lens_report.rb +99 -0
- data/lib/postscript/level_violation.rb +6 -0
- data/lib/postscript/model/operator.rb +12 -1
- data/lib/postscript/model/operators.rb +3 -0
- data/lib/postscript/model/vocabulary.rb +68 -0
- data/lib/postscript/model.rb +3 -2
- data/lib/postscript/report.rb +31 -0
- data/lib/postscript/serializer.rb +50 -7
- data/lib/postscript/source/ast_builder.rb +17 -22
- data/lib/postscript/source/lexer.rb +126 -57
- data/lib/postscript/source/native_engine.rb +125 -0
- data/lib/postscript/source/native_lexer.rb +185 -0
- data/lib/postscript/source.rb +22 -2
- data/lib/postscript/validation/grammar.rb +51 -0
- data/lib/postscript/validation/levels.rb +76 -0
- data/lib/postscript/validation/vocabulary.rb +42 -0
- data/lib/postscript/validation.rb +10 -0
- data/lib/postscript/validator.rb +59 -0
- data/lib/postscript/version.rb +1 -1
- data/lib/postscript.rb +34 -4
- metadata +23 -2
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
module Source
|
|
5
|
+
# Tokenizes PS source with the vendored PARG grammar on parsanol's
|
|
6
|
+
# native engine: Source.parse(source, engine: :native).
|
|
7
|
+
#
|
|
8
|
+
# The result is the Model::Token stream Lexer produces for the same
|
|
9
|
+
# source: the same types, values (with Lexer's encodings) and 1-based
|
|
10
|
+
# line/column positions counted in bytes. Where the grammar rejects
|
|
11
|
+
# the source, Lexer tokenizes it instead, so errors, and Lexer's
|
|
12
|
+
# tolerance of input the grammar rejects, are Lexer's.
|
|
13
|
+
class NativeLexer
|
|
14
|
+
# Captures that are one token each: capture name => token type.
|
|
15
|
+
WORDS = {
|
|
16
|
+
integer: :number, real: :number, radix: :operator, name: :operator
|
|
17
|
+
}.freeze
|
|
18
|
+
|
|
19
|
+
# Composite captures => [open token, close token].
|
|
20
|
+
COMPOSITES = {
|
|
21
|
+
array: [[:array_open, "["], [:array_close, "]"]],
|
|
22
|
+
procedure: [[:proc_open, "{"], [:proc_close, "}"]],
|
|
23
|
+
dictionary: [[:dict_open, "<<"], [:dict_close, ">>"]],
|
|
24
|
+
}.freeze
|
|
25
|
+
|
|
26
|
+
def self.tokenize(source)
|
|
27
|
+
new(source).tokenize
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def initialize(source)
|
|
31
|
+
@source = source.to_s
|
|
32
|
+
@bytes = @source.b
|
|
33
|
+
@input = Input.new(@bytes)
|
|
34
|
+
@lines = Lines.new(@bytes)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def tokenize
|
|
38
|
+
tree = NativeEngine.parse(@input.text)
|
|
39
|
+
return Lexer.tokenize(@source) if tree.nil?
|
|
40
|
+
|
|
41
|
+
@tokens = []
|
|
42
|
+
walk(tree)
|
|
43
|
+
@tokens.freeze
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
# Arrays hold captures in source order; a slice is uncaptured text
|
|
49
|
+
# (whitespace and comments).
|
|
50
|
+
def walk(node)
|
|
51
|
+
case node
|
|
52
|
+
when Array then node.each { |child| walk(child) }
|
|
53
|
+
when Hash then node.each { |kind, value| emit(kind, value) }
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def emit(kind, value)
|
|
58
|
+
if (type = WORDS[kind])
|
|
59
|
+
token(type, word(slice(value)), value)
|
|
60
|
+
elsif (delimiters = COMPOSITES[kind])
|
|
61
|
+
composite(delimiters, value)
|
|
62
|
+
else
|
|
63
|
+
emit_other(kind, value)
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def emit_other(kind, value)
|
|
68
|
+
case kind
|
|
69
|
+
when :literal_name then emit_literal_name(value)
|
|
70
|
+
when :string then emit_string(value)
|
|
71
|
+
when :hex_string then emit_hex_string(value)
|
|
72
|
+
when :dsc then emit_dsc(value)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def emit_literal_name(value)
|
|
77
|
+
token(:name, word(slice(value).byteslice(1..)), value, literal: true)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def emit_string(value)
|
|
81
|
+
raw = slice(value).byteslice(1...-1)
|
|
82
|
+
token(:string, Lexer.decode_string(raw), value)
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def emit_hex_string(value)
|
|
86
|
+
raw = slice(value).byteslice(1...-1)
|
|
87
|
+
token(:hexstring, word(Lexer.decode_hex(raw)), value)
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Lexer reports a DSC comment from after its "%%", in the source's
|
|
91
|
+
# encoding, and reports none for a "%%" that ends the source.
|
|
92
|
+
def emit_dsc(value)
|
|
93
|
+
range = span(value)
|
|
94
|
+
return if range.size == 2 && range.end == @bytes.bytesize
|
|
95
|
+
|
|
96
|
+
start = range.begin + 2
|
|
97
|
+
add(:dsc, @source.byteslice(start, range.size - 2).chomp, start)
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
def composite((open, close), value)
|
|
101
|
+
add(*open, span(value[:open]).begin)
|
|
102
|
+
walk(value[:items])
|
|
103
|
+
add(*close, span(value[:close]).begin)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def token(type, text, value, literal: false)
|
|
107
|
+
add(type, text, @input.offset(value.offset), literal: literal)
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def add(type, value, offset, literal: false)
|
|
111
|
+
line, column = @lines.position(offset)
|
|
112
|
+
@tokens << Model::Token.new(type, value, line: line, column: column,
|
|
113
|
+
literal: literal)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# The source bytes a capture covers.
|
|
117
|
+
def slice(value)
|
|
118
|
+
range = span(value)
|
|
119
|
+
@bytes.byteslice(range.begin, range.size)
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def span(value)
|
|
123
|
+
start = @input.offset(value.offset)
|
|
124
|
+
start...@input.offset(value.offset + value.to_s.bytesize)
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Lexer builds word values byte by byte: UTF-8 when ASCII-only,
|
|
128
|
+
# binary otherwise.
|
|
129
|
+
def word(raw)
|
|
130
|
+
raw.ascii_only? ? raw.force_encoding(Encoding::UTF_8) : raw.b
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
# The source as parsanol reads it: UTF-8. A source with non-ASCII
|
|
134
|
+
# bytes is read as Latin-1, so every byte is one character whatever
|
|
135
|
+
# the encoding, and offsets are mapped back to source bytes. Shared
|
|
136
|
+
# with Validation::Grammar for native diagnostic coordinates.
|
|
137
|
+
class Input
|
|
138
|
+
attr_reader :text
|
|
139
|
+
|
|
140
|
+
def initialize(bytes)
|
|
141
|
+
if bytes.ascii_only?
|
|
142
|
+
@text = bytes.dup.force_encoding(Encoding::UTF_8)
|
|
143
|
+
@widened = []
|
|
144
|
+
else
|
|
145
|
+
latin1 = bytes.dup.force_encoding(Encoding::ISO_8859_1)
|
|
146
|
+
@text = latin1.encode(Encoding::UTF_8)
|
|
147
|
+
@widened = widened_offsets(bytes)
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
# The source byte offset of +text+ byte offset +offset+.
|
|
152
|
+
def offset(offset)
|
|
153
|
+
before = @widened.bsearch_index { |at| at >= offset }
|
|
154
|
+
offset - (before || @widened.size)
|
|
155
|
+
end
|
|
156
|
+
|
|
157
|
+
private
|
|
158
|
+
|
|
159
|
+
# Offsets in +text+ of the characters that take two bytes there.
|
|
160
|
+
def widened_offsets(bytes)
|
|
161
|
+
offsets = []
|
|
162
|
+
bytes.each_byte.with_index do |byte, index|
|
|
163
|
+
offsets << (index + offsets.size) if byte >= 0x80
|
|
164
|
+
end
|
|
165
|
+
offsets
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
|
|
169
|
+
# Lexer's positions: lines end at LF; columns count bytes from 1.
|
|
170
|
+
class Lines
|
|
171
|
+
def initialize(bytes)
|
|
172
|
+
@newlines = []
|
|
173
|
+
index = -1
|
|
174
|
+
@newlines << index while (index = bytes.index("\n", index + 1))
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def position(offset)
|
|
178
|
+
line = @newlines.bsearch_index { |at| at >= offset } || @newlines.size
|
|
179
|
+
start = line.zero? ? 0 : @newlines[line - 1] + 1
|
|
180
|
+
[line + 1, offset - start + 1]
|
|
181
|
+
end
|
|
182
|
+
end
|
|
183
|
+
end
|
|
184
|
+
end
|
|
185
|
+
end
|
data/lib/postscript/source.rb
CHANGED
|
@@ -6,16 +6,36 @@ module Postscript
|
|
|
6
6
|
# Lexer produces an array of Model::Token; AstBuilder walks tokens and
|
|
7
7
|
# emits a typed Model::Program. Errors are raised as Postscript::LexError
|
|
8
8
|
# and Postscript::SyntaxError respectively.
|
|
9
|
+
#
|
|
10
|
+
# +engine:+ picks the tokenizer: +:ruby+ (the default) is Lexer;
|
|
11
|
+
# +:native+ is NativeLexer, the PARG grammar on parsanol's native
|
|
12
|
+
# engine, which needs the optional parsanol gem and produces the same
|
|
13
|
+
# tokens.
|
|
9
14
|
module Source
|
|
10
15
|
autoload :Lexer, "postscript/source/lexer"
|
|
16
|
+
autoload :NativeLexer, "postscript/source/native_lexer"
|
|
17
|
+
autoload :NativeEngine, "postscript/source/native_engine"
|
|
11
18
|
autoload :AstBuilder, "postscript/source/ast_builder"
|
|
12
19
|
autoload :OperandStack, "postscript/source/operand_stack"
|
|
13
20
|
|
|
21
|
+
ENGINES = %i[ruby native].freeze
|
|
22
|
+
|
|
14
23
|
module_function
|
|
15
24
|
|
|
16
25
|
# One-shot convenience: PS source -> Model::Program.
|
|
17
|
-
def parse(source)
|
|
18
|
-
AstBuilder.build(
|
|
26
|
+
def parse(source, engine: :ruby)
|
|
27
|
+
AstBuilder.build(tokenize(source, engine: engine))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# PS source -> array of Model::Token.
|
|
31
|
+
def tokenize(source, engine: :ruby)
|
|
32
|
+
case engine
|
|
33
|
+
when :ruby then Lexer.tokenize(source)
|
|
34
|
+
when :native then NativeLexer.tokenize(source)
|
|
35
|
+
else
|
|
36
|
+
raise ArgumentError,
|
|
37
|
+
"unknown engine #{engine.inspect} (use one of #{ENGINES.inspect})"
|
|
38
|
+
end
|
|
19
39
|
end
|
|
20
40
|
end
|
|
21
41
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
module Validation
|
|
5
|
+
# Converts Ruby parse failures or native PARG failures into diagnostics.
|
|
6
|
+
module Grammar
|
|
7
|
+
class << self
|
|
8
|
+
def check(source, engine: :ruby)
|
|
9
|
+
source = source.to_s
|
|
10
|
+
if engine == :auto
|
|
11
|
+
engine = Source::NativeEngine.available? ? :native : :ruby
|
|
12
|
+
end
|
|
13
|
+
return native_check(source) if engine == :native
|
|
14
|
+
|
|
15
|
+
Source.parse(source, engine: engine)
|
|
16
|
+
[]
|
|
17
|
+
rescue ParseError => e
|
|
18
|
+
line, column = e.source_position
|
|
19
|
+
[Diagnostic.new(code: :syntax, kind: :grammar, severity: :error,
|
|
20
|
+
message: e.message, line: line, column: column,
|
|
21
|
+
offset: byte_offset(source, line, column),
|
|
22
|
+
rule: nil, expected: [])]
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
private
|
|
26
|
+
|
|
27
|
+
def native_check(source)
|
|
28
|
+
bytes = source.b
|
|
29
|
+
input = Source::NativeLexer::Input.new(bytes)
|
|
30
|
+
failure = Source::NativeEngine.diagnose(input.text)
|
|
31
|
+
return [] unless failure
|
|
32
|
+
|
|
33
|
+
offset = input.offset(failure[:offset]) if failure[:offset]
|
|
34
|
+
if offset
|
|
35
|
+
line, column = Source::NativeLexer::Lines.new(bytes).position(offset)
|
|
36
|
+
end
|
|
37
|
+
[Diagnostic.new(code: :syntax, kind: :grammar, severity: :error,
|
|
38
|
+
message: failure[:message], offset: offset,
|
|
39
|
+
line: line, column: column, rule: failure[:rule],
|
|
40
|
+
expected: failure[:expected])]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def byte_offset(source, line, column)
|
|
44
|
+
return unless line && column
|
|
45
|
+
|
|
46
|
+
source.each_line("\n").take(line - 1).sum(&:bytesize) + column - 1
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
module Validation
|
|
5
|
+
# Finds operators that need a newer PostScript language level than
|
|
6
|
+
# the target level. An operator's level is the first level that has
|
|
7
|
+
# it; names with no vocabulary entry and calls to user procedures are
|
|
8
|
+
# never violations.
|
|
9
|
+
#
|
|
10
|
+
# An explicit +level:+ always wins and must be 1, 2 or 3. Without it,
|
|
11
|
+
# the program's %%LanguageLevel header is used; a missing header, or
|
|
12
|
+
# one outside 1..3 (untrusted input, so ignored rather than raised
|
|
13
|
+
# on), gives level 3.
|
|
14
|
+
module Levels
|
|
15
|
+
LEVELS = (1..3)
|
|
16
|
+
DEFAULT_LEVEL = 3
|
|
17
|
+
|
|
18
|
+
class << self
|
|
19
|
+
def violations(program, level: nil)
|
|
20
|
+
level = resolve_level(program, level)
|
|
21
|
+
|
|
22
|
+
result = []
|
|
23
|
+
program.body.each { |node| walk(node, level, result) }
|
|
24
|
+
result.freeze
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
private
|
|
28
|
+
|
|
29
|
+
def resolve_level(program, level)
|
|
30
|
+
if level.nil?
|
|
31
|
+
header_level = program.header.language_level
|
|
32
|
+
return valid?(header_level) ? header_level : DEFAULT_LEVEL
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
unless valid?(level)
|
|
36
|
+
raise ArgumentError, "level must be 1, 2 or 3, got #{level.inspect}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
level
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def valid?(level)
|
|
43
|
+
level.is_a?(Integer) && LEVELS.cover?(level)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def walk(node, level, result)
|
|
47
|
+
if node.is_a?(Model::Operator)
|
|
48
|
+
check(node, level, result)
|
|
49
|
+
else
|
|
50
|
+
children(node).each { |child| walk(child, level, result) }
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# Calls to user procedures are never violations: InvokeProcedure
|
|
55
|
+
# has no language level, and the body is checked where it is
|
|
56
|
+
# defined, as a Procedure literal.
|
|
57
|
+
def check(operator, level, result)
|
|
58
|
+
needed = operator.language_level
|
|
59
|
+
return unless needed && needed > level
|
|
60
|
+
|
|
61
|
+
result << LevelViolation.new(keyword: operator.keyword,
|
|
62
|
+
level: needed).freeze
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def children(node)
|
|
66
|
+
case node
|
|
67
|
+
when Model::Literals::Procedure then node.body
|
|
68
|
+
when Model::Literals::ArrayLiteral then node.elements
|
|
69
|
+
when Model::Literals::Dictionary then node.entries.to_a.flatten(1)
|
|
70
|
+
else []
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
module Validation
|
|
5
|
+
# Finds names absent from the vocabulary and the program's def bindings.
|
|
6
|
+
# This is a static check: bindings anywhere in the body are considered,
|
|
7
|
+
# without simulating execution or dictionary scope.
|
|
8
|
+
module Vocabulary
|
|
9
|
+
class << self
|
|
10
|
+
def unknown_names(program)
|
|
11
|
+
names = {}
|
|
12
|
+
bindings = {}
|
|
13
|
+
program.body.each { |node| walk(node, names, bindings) }
|
|
14
|
+
names.keys.reject { |name| bindings.key?(name) }.freeze
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def walk(node, names, bindings)
|
|
20
|
+
case node
|
|
21
|
+
when Model::UnknownOperator
|
|
22
|
+
names[node.keyword] = true unless Model::Vocabulary.entry(node.keyword)
|
|
23
|
+
when Model::Operators::Dictionary::Def
|
|
24
|
+
key = node.key
|
|
25
|
+
bindings[key.value] = true if key.is_a?(Model::Literals::Name)
|
|
26
|
+
else
|
|
27
|
+
children(node).each { |child| walk(child, names, bindings) }
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def children(node)
|
|
32
|
+
case node
|
|
33
|
+
when Model::Literals::Procedure then node.body
|
|
34
|
+
when Model::Literals::ArrayLiteral then node.elements
|
|
35
|
+
when Model::Literals::Dictionary then node.entries.to_a.flatten(1)
|
|
36
|
+
else []
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
# Checks on the typed PostScript model.
|
|
5
|
+
module Validation
|
|
6
|
+
autoload :Grammar, "postscript/validation/grammar"
|
|
7
|
+
autoload :Levels, "postscript/validation/levels"
|
|
8
|
+
autoload :Vocabulary, "postscript/validation/vocabulary"
|
|
9
|
+
end
|
|
10
|
+
end
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Postscript
|
|
4
|
+
# Combines source and model checks, stopping at invalid grammar.
|
|
5
|
+
module Validator
|
|
6
|
+
class << self
|
|
7
|
+
def call(source, level: nil, engine: :auto, timestamp: nil)
|
|
8
|
+
if engine == :auto
|
|
9
|
+
engine = Source::NativeEngine.available? ? :native : :ruby
|
|
10
|
+
end
|
|
11
|
+
report, target = validation_report(source, level, engine)
|
|
12
|
+
LensReport.call(report, level: target, engine: engine,
|
|
13
|
+
timestamp: timestamp)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
private
|
|
17
|
+
|
|
18
|
+
def validation_report(source, level, engine)
|
|
19
|
+
diagnostics = Validation::Grammar.check(source, engine: engine)
|
|
20
|
+
unless diagnostics.empty?
|
|
21
|
+
return [Validation::Report.new(diagnostics: diagnostics), level || 3]
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
program = Source.parse(source, engine: engine)
|
|
25
|
+
level = resolved_level(program, level)
|
|
26
|
+
diagnostics = level_diagnostics(program, level)
|
|
27
|
+
diagnostics.concat(unknown_diagnostics(program))
|
|
28
|
+
[Validation::Report.new(diagnostics: diagnostics), level]
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def unknown_diagnostics(program)
|
|
32
|
+
Validation::Vocabulary.unknown_names(program).map do |name|
|
|
33
|
+
Diagnostic.new(
|
|
34
|
+
code: :unknown_name, kind: :unknown_name,
|
|
35
|
+
severity: :warning, message: "Unknown name: #{name}"
|
|
36
|
+
)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def resolved_level(program, level)
|
|
41
|
+
return level unless level.nil?
|
|
42
|
+
|
|
43
|
+
header = program.header.language_level
|
|
44
|
+
(1..3).cover?(header) ? header : 3
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def level_diagnostics(program, level)
|
|
48
|
+
Validation::Levels.violations(program, level: level).map do |violation|
|
|
49
|
+
Diagnostic.new(
|
|
50
|
+
code: :level, kind: :level, severity: :error,
|
|
51
|
+
message: "#{violation.keyword} is Level #{violation.level}; " \
|
|
52
|
+
"this program targets Level #{level}",
|
|
53
|
+
entry: Model::Vocabulary.entry(violation.keyword)
|
|
54
|
+
)
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
data/lib/postscript/version.rb
CHANGED
data/lib/postscript.rb
CHANGED
|
@@ -8,6 +8,7 @@ module Postscript
|
|
|
8
8
|
autoload :ParseError, "postscript/errors"
|
|
9
9
|
autoload :LexError, "postscript/errors"
|
|
10
10
|
autoload :SyntaxError, "postscript/errors"
|
|
11
|
+
autoload :NativeEngineUnavailable, "postscript/errors"
|
|
11
12
|
autoload :RenderError, "postscript/errors"
|
|
12
13
|
autoload :StackUnderflowError, "postscript/errors"
|
|
13
14
|
autoload :UndefinedOperatorError, "postscript/errors"
|
|
@@ -21,12 +22,18 @@ module Postscript
|
|
|
21
22
|
autoload :FormatNumber, "postscript/format_number"
|
|
22
23
|
autoload :Matrix, "postscript/matrix"
|
|
23
24
|
autoload :Color, "postscript/color"
|
|
25
|
+
autoload :LevelViolation, "postscript/level_violation"
|
|
26
|
+
autoload :Diagnostic, "postscript/diagnostic"
|
|
24
27
|
|
|
25
28
|
# PS source-reading layer
|
|
26
29
|
autoload :Source, "postscript/source"
|
|
27
30
|
|
|
28
31
|
# Typed PS domain model
|
|
29
32
|
autoload :Model, "postscript/model"
|
|
33
|
+
autoload :Validation, "postscript/validation"
|
|
34
|
+
autoload :Validator, "postscript/validator"
|
|
35
|
+
autoload :LensReport, "postscript/lens_report"
|
|
36
|
+
Validation.autoload :Report, "postscript/report"
|
|
30
37
|
|
|
31
38
|
# PS source serializer
|
|
32
39
|
autoload :Serializer, "postscript/serializer"
|
|
@@ -42,8 +49,11 @@ module Postscript
|
|
|
42
49
|
# Example:
|
|
43
50
|
# program = Postscript.parse(ps_source)
|
|
44
51
|
# program.body.each { |node| puts node.class }
|
|
45
|
-
|
|
46
|
-
|
|
52
|
+
#
|
|
53
|
+
# +engine: :native+ tokenizes on parsanol's native engine (needs the
|
|
54
|
+
# optional parsanol gem); see Source.
|
|
55
|
+
def parse(source, engine: :ruby)
|
|
56
|
+
Source.parse(source, engine: engine)
|
|
47
57
|
end
|
|
48
58
|
|
|
49
59
|
# One-shot: +Model::Program+ -> PS source string.
|
|
@@ -54,10 +64,30 @@ module Postscript
|
|
|
54
64
|
Serializer.call(program, **opts)
|
|
55
65
|
end
|
|
56
66
|
|
|
67
|
+
# One-shot: +Model::Program+ -> array of +LevelViolation+, one per
|
|
68
|
+
# operator that needs a newer language level than +level+. Reports
|
|
69
|
+
# and never raises for a parsed program; see Validation::Levels for
|
|
70
|
+
# how the level is chosen when +level:+ is nil. An explicit level
|
|
71
|
+
# outside 1..3 raises ArgumentError.
|
|
72
|
+
#
|
|
73
|
+
# Example:
|
|
74
|
+
# program = Postscript.parse("0 0 moveto shfill")
|
|
75
|
+
# Postscript.level_violations(program, level: 2)
|
|
76
|
+
# # => [#<struct Postscript::LevelViolation keyword="shfill", level=3>]
|
|
77
|
+
def level_violations(program, level: nil)
|
|
78
|
+
Validation::Levels.violations(program, level: level)
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Check grammar, language level and unknown names in one report.
|
|
82
|
+
# Auto uses the native engine when available, otherwise Ruby.
|
|
83
|
+
def validate(source, level: nil, engine: :auto, timestamp: nil)
|
|
84
|
+
Validator.call(source, level: level, engine: engine, timestamp: timestamp)
|
|
85
|
+
end
|
|
86
|
+
|
|
57
87
|
# One-shot: PS source string -> array of +Model::Token+.
|
|
58
88
|
# Useful for debugging or building alternative parsers.
|
|
59
|
-
def tokenize(source)
|
|
60
|
-
Source
|
|
89
|
+
def tokenize(source, engine: :ruby)
|
|
90
|
+
Source.tokenize(source, engine: engine)
|
|
61
91
|
end
|
|
62
92
|
end
|
|
63
93
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: postscript
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.2.
|
|
4
|
+
version: 0.2.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ribose Inc.
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-23 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: thor
|
|
@@ -44,12 +44,24 @@ files:
|
|
|
44
44
|
- LICENSE
|
|
45
45
|
- README.adoc
|
|
46
46
|
- Rakefile
|
|
47
|
+
- data/guide_operators.yml
|
|
48
|
+
- data/operator_overrides.yml
|
|
49
|
+
- docs/operators.adoc
|
|
50
|
+
- docs/plans/issue-4-native-parser.md
|
|
47
51
|
- exe/postscript
|
|
52
|
+
- grammars/README.md
|
|
53
|
+
- grammars/postscript.json
|
|
54
|
+
- grammars/postscript.parg
|
|
55
|
+
- grammars/upstream.yml
|
|
48
56
|
- lib/postscript.rb
|
|
49
57
|
- lib/postscript/cli.rb
|
|
50
58
|
- lib/postscript/color.rb
|
|
59
|
+
- lib/postscript/diagnostic.rb
|
|
51
60
|
- lib/postscript/errors.rb
|
|
52
61
|
- lib/postscript/format_number.rb
|
|
62
|
+
- lib/postscript/lens_citations.rb
|
|
63
|
+
- lib/postscript/lens_report.rb
|
|
64
|
+
- lib/postscript/level_violation.rb
|
|
53
65
|
- lib/postscript/matrix.rb
|
|
54
66
|
- lib/postscript/model.rb
|
|
55
67
|
- lib/postscript/model/literals.rb
|
|
@@ -77,11 +89,20 @@ files:
|
|
|
77
89
|
- lib/postscript/model/operators/transformations.rb
|
|
78
90
|
- lib/postscript/model/program.rb
|
|
79
91
|
- lib/postscript/model/token.rb
|
|
92
|
+
- lib/postscript/model/vocabulary.rb
|
|
93
|
+
- lib/postscript/report.rb
|
|
80
94
|
- lib/postscript/serializer.rb
|
|
81
95
|
- lib/postscript/source.rb
|
|
82
96
|
- lib/postscript/source/ast_builder.rb
|
|
83
97
|
- lib/postscript/source/lexer.rb
|
|
98
|
+
- lib/postscript/source/native_engine.rb
|
|
99
|
+
- lib/postscript/source/native_lexer.rb
|
|
84
100
|
- lib/postscript/source/operand_stack.rb
|
|
101
|
+
- lib/postscript/validation.rb
|
|
102
|
+
- lib/postscript/validation/grammar.rb
|
|
103
|
+
- lib/postscript/validation/levels.rb
|
|
104
|
+
- lib/postscript/validation/vocabulary.rb
|
|
105
|
+
- lib/postscript/validator.rb
|
|
85
106
|
- lib/postscript/version.rb
|
|
86
107
|
homepage: https://github.com/claricle/postscript
|
|
87
108
|
licenses:
|