Skip to content

Commit

Permalink
Correct minor errors in the Lustre lexer (#1316)
Browse files Browse the repository at this point in the history
This commit primarily adds some missing frozen string literal pragmas
and makes regular expression literals more consistent.
  • Loading branch information
pyrmont authored Sep 1, 2019
1 parent 8168673 commit 9724e00
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
45 changes: 22 additions & 23 deletions lib/rouge/lexers/lustre.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*- #
#
# adapted from ocaml.rb, hence some ocaml-ism migth remains
# frozen_string_literal: true

module Rouge
module Lexers
class Lustre < RegexLexer
Expand All @@ -12,16 +12,16 @@ class Lustre < RegexLexer

def self.keywords
@keywords ||= Set.new %w(
extern unsafe assert const current enum function
let node operator returns
step struct tel type var model package needs
provides uses is body end include merge
extern unsafe assert const current enum function let node operator
returns step struct tel type var model package needs provides uses is
body end include merge
)
end

def self.word_operators
@word_operators ||= Set.new %w(
div and xor mod or not nor if then else fby pre when with)
div and xor mod or not nor if then else fby pre when with
)
end

def self.primitives
Expand All @@ -32,8 +32,8 @@ def self.primitives
id = /[a-z_][\w']*/i

state :root do
rule (/\s+/m), Text
rule (/false|true/), Keyword::Constant
rule %r/\s+/m, Text
rule %r/false|true/, Keyword::Constant
rule %r(\-\-.*), Comment::Single
rule %r(/\*.*?\*/)m, Comment::Multiline
rule %r(\(\*.*?\*\))m, Comment::Multiline
Expand All @@ -50,31 +50,30 @@ def self.primitives
end
end

rule (/[(){}\[\];]+/), Punctuation
rule %r/[(){}\[\];]+/, Punctuation
rule operator, Operator

rule (/-?\d[\d_]*(.[\d_]*)?(e[+-]?\d[\d_]*)/i), Num::Float
rule (/\d[\d_]*/), Num::Integer
rule %r/-?\d[\d_]*(.[\d_]*)?(e[+-]?\d[\d_]*)/i, Num::Float
rule %r/\d[\d_]*/, Num::Integer

rule (/'(?:(\\[\\"'ntbr ])|(\\[0-9]{3})|(\\x\h{2}))'/), Str::Char
rule (/'[.]'/), Str::Char
rule (/"/), Str::Double, :string
rule (/[~?]#{id}/), Name::Variable
rule %r/'(?:(\\[\\"'ntbr ])|(\\[0-9]{3})|(\\x\h{2}))'/, Str::Char
rule %r/'[.]'/, Str::Char
rule %r/"/, Str::Double, :string
rule %r/[~?]#{id}/, Name::Variable
end

state :string do
rule (/[^\\"]+/), Str::Double
rule %r/[^\\"]+/, Str::Double
mixin :escape_sequence
rule (/\\\n/), Str::Double
rule (/"/), Str::Double, :pop!
rule %r/\\\n/, Str::Double
rule %r/"/, Str::Double, :pop!
end

state :escape_sequence do
rule (/\\[\\"'ntbr]/), Str::Escape
rule (/\\\d{3}/), Str::Escape
rule (/\\x\h{2}/), Str::Escape
rule %r/\\[\\"'ntbr]/, Str::Escape
rule %r/\\\d{3}/, Str::Escape
rule %r/\\x\h{2}/, Str::Escape
end

end
end
end
2 changes: 1 addition & 1 deletion spec/lexers/lustre_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::Lustre do
let(:subject) { Rouge::Lexers::Lustre.new }
Expand All @@ -15,4 +16,3 @@
end
end
end

4 changes: 2 additions & 2 deletions spec/visual/samples/lustre
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

-- Constants
const size = 50; -- size of the sliding windows used to smooth values
const period = 0.1; -- in seconds
const speed_max = 110.0; -- comment with leading whitespace
const period = 0.1; -- in seconds
const speed_max = 110.0; -- comment with leading whitespace

-- Types
type roof_state = enum { locked, in_motion };
Expand Down

0 comments on commit 9724e00

Please sign in to comment.