Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New lexer: lustre #905

Merged
merged 20 commits into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
10ce91f
New lexer: lustre
jahierwan Apr 26, 2018
31b6f45
New lexer: lustre (commiting the good files)
jahierwan Apr 26, 2018
923d69b
New lexer: lutin
jahierwan Apr 26, 2018
7663fc5
Shorten the Lutin example.
Jan 14, 2019
cc5c91c
New lexer: lustre
jahierwan Apr 26, 2018
ce9836b
New lexer: lustre (commiting the good files)
jahierwan Apr 26, 2018
8eaae46
New lexer: lutin
jahierwan Apr 26, 2018
837ed67
Shorten the Lutin example.
Jan 14, 2019
a440f40
Merge branch 'master' of https://github.com/jahierwan/rouge
jahierwan Aug 19, 2019
c26b24b
Remove lutin support; I will submit them in a different PR as request…
jahierwan Aug 19, 2019
b1c19c7
Cut down the demos/lustre example to a 5 lines program (#discussion_r…
jahierwan Aug 19, 2019
4228d11
true and false should be Keyword::Constant, not Name::Builtin::Pseudo…
jahierwan Aug 19, 2019
973aa8b
Don't worry about end of line in comments (#discussion_r310300155)
jahierwan Aug 19, 2019
942d79e
Remove useless rules (#discussion_r310300940, #discussion_r310302349)
jahierwan Aug 19, 2019
7bec098
Remove support for Hex and octal number (as they are not supported in…
jahierwan Aug 19, 2019
d130378
fix an error (973aa8bf5fa995be9) and remove warnings
jahierwan Aug 19, 2019
3ee37a6
Remove ';' and '-' from the list of operators, and remove 'true' and …
jahierwan Aug 20, 2019
8c6532e
Remove the unused dotted state
jahierwan Aug 20, 2019
3c43e62
Shorten the visual lustre example
jahierwan Aug 22, 2019
f6a1d7d
Rationalise visual sample
pyrmont Aug 23, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lib/rouge/demos/lustre
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const nbtrains = 3;

node switch(init, set, reset: bool) returns (state: bool);
-- from an initial value (init), the state is set to true or reset to false
-- and keeps its previous value otherwise
let
state = init -> if set and not pre(state) then true
else if reset and pre(state) then false
else pre(state);
tel;

node count(init: int; reset, event: bool) returns (c: int);
let
c = init -> if reset then init
else if event then pre(c)+1
else pre(c);
tel;

node train (s,b: bool)
returns (ontime, onbrake, late, stopped: bool);
-- a train receives signals "second" and "beacon" (assumed to be exclusive)
-- and returns its state.
var delta, nbrake: int;
let
assert #(s,b);
assert (true -> (pre(stopped) => not b));
-- delta = nb_seconds - nb_beacons
delta = 0 -> pre(delta) +
if s then 1 else if b then -1 else 0;
ontime = true -> not (late or onbrake or stopped);
late = switch(false, delta>=10, delta<=0);
-- counting beacons when on-brake or stopped
nbrake = count(0, not(onbrake or stopped), b);
--early = onbrake or stopped;
onbrake = switch(false, delta<=-10 and pre(nbrake)<10, delta>=0 or pre(nbrake)>= 10);
stopped = switch(false, pre(onbrake) and pre(nbrake)>= 10, delta>=0);
tel;

node clock (late: bool^nbtrains; ss: bool) returns (s: bool);
-- the clock broadcasts the "second" only when no train is late
var
onelatetrain: bool;
let
onelatetrain = red<<or;nbtrains>>(false, late);
s = if onelatetrain then false else ss;
tel

node metro (sec: bool; beacons: bool^nbtrains)
returns (ontime, onbrake, late, stopped: bool^nbtrains);
var second : bool;
let
second = clock(late, sec);
(ontime, onbrake, late, stopped) = map<<train;nbtrains>> (sec^nbtrains, beacons);
tel
jahierwan marked this conversation as resolved.
Show resolved Hide resolved
29 changes: 29 additions & 0 deletions lib/rouge/demos/lutin
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
-- Some of the examples provided in [SIES'13] "Engineering Functional
-- Requirements of Reactive Systems using Synchronous Languages"

let inertia=0.6
node gen_x_v2() returns (x:real) = loop { 0.0<x and x<42.0 fby loop [20] x = pre x }

node gen_x_v4() returns (target:real; x:real=0.0) =
run target := gen_x_v2() in
exist px,ppx : real = 0.0 in
loop {
px = pre x and ppx = pre px and
x = (px+target) / 2.0+inertia*(px-ppx)
}

-- saw-tooth curves
let Between(x, min, max : real) : bool = ((min < x) and (x < max))

node up(init, delta:real) returns( x : real) =
x = init fby loop { Between(x, pre x, pre x + delta) }
node down(init, delta:real) returns( x : real) =
x = init fby loop { Between(x, pre x - delta, pre x) }

node up_and_down(min, max, delta : real) returns (x : real) =
Between(x, min, max)
fby
loop {
| run x := up(pre x, delta) in loop { x < max }
| run x := down(pre x, delta) in loop { x > min }
}
90 changes: 90 additions & 0 deletions lib/rouge/lexers/lustre.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*- #
#
# adapted from ocaml.rb, hence some ocaml-ism migth remains
module Rouge
module Lexers
class Lustre < RegexLexer
title "Lustre"
desc 'The Lustre programming language (Verimag)'
tag 'lustre'
filenames '*.lus'
mimetypes 'text/x-lustre'

def self.keywords
@keywords ||= Set.new %w(
extern unsafe assert const current enum function
false let node operator returns
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
step struct tel type true 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)
end

def self.primitives
@primitives ||= Set.new %w(int real bool)
end

operator = %r([;,_!$%&*+./:<=>?@^|~#-]+)
id = /[a-z_][\w']*/i

state :root do
rule /\s+/m, Text
rule /false|true|[(][)]|\[\]/, Name::Builtin::Pseudo
jahierwan marked this conversation as resolved.
Show resolved Hide resolved
rule %r(\-\-.*?$), Comment::Single
jahierwan marked this conversation as resolved.
Show resolved Hide resolved
rule %r(/\*.*?\*/)m, Comment::Multiline
rule %r(\(\*.*?\*\))m, Comment::Multiline
rule id do |m|
match = m[0]
if self.class.keywords.include? match
token Keyword
elsif self.class.word_operators.include? match
token Operator::Word
elsif self.class.primitives.include? match
token Keyword::Type
else
token Name
end
end

rule /[(){}\[\];]+/, Punctuation
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule operator, Operator

rule /-?\d[\d_]*(.[\d_]*)?(e[+-]?\d[\d_]*)/i, Num::Float
rule /0x\h[\h_]*/i, Num::Hex
rule /0o[0-7][0-7_]*/i, Num::Oct
rule /0b[01][01_]*/i, Num::Bin
rule /\d[\d_]*/, Num::Integer

rule /'(?:(\\[\\"'ntbr ])|(\\[0-9]{3})|(\\x\h{2}))'/, Str::Char
rule /'[.]'/, Str::Char
rule /'/, Keyword
jahierwan marked this conversation as resolved.
Show resolved Hide resolved
rule /"/, Str::Double, :string
rule /[~?]#{id}/, Name::Variable
end

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

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

state :dotted do
rule /\s+/m, Text
rule /[.]/, Punctuation
rule id, Name, :pop!
rule /[({\[]/, Punctuation, :pop!
end
end
end
end
88 changes: 88 additions & 0 deletions lib/rouge/lexers/lutin.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
# -*- coding: utf-8 -*- #
#
# adapted from lustre.rf (adapted from ocaml.rb), hence some ocaml-ism migth remains
module Rouge
module Lexers
class Lutin < RegexLexer
title "Lutin"
desc 'The Lutin programming language (Verimag)'
tag 'lutin'
filenames '*.lut'
mimetypes 'text/x-lutin'

def self.keywords
@keywords ||= Set.new %w(
let in node extern system returns weak strong assert raise try catch
trap do exist erun run type ref exception include false true
)
end

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

def self.primitives
@primitives ||= Set.new %w(int real bool trace loop fby)
end

operator = %r([;,_!$%&*+./:<=>?@^|~#-]+)
id = /[a-z_][\w']*/i

state :root do
rule /\s+/m, Text
rule /false|true|[(][)]|\[\]/, Name::Builtin::Pseudo
rule %r(\-\-.*?$), Comment::Single
rule %r(/\*.*?\*/)m, Comment::Multiline
rule %r(\(\*.*?\*\))m, Comment::Multiline
rule id do |m|
match = m[0]
if self.class.keywords.include? match
token Keyword
elsif self.class.word_operators.include? match
token Operator::Word
elsif self.class.primitives.include? match
token Keyword::Type
else
token Name
end
end

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

rule /-?\d[\d_]*(.[\d_]*)?(e[+-]?\d[\d_]*)/i, Num::Float
rule /0x\h[\h_]*/i, Num::Hex
rule /0o[0-7][0-7_]*/i, Num::Oct
rule /0b[01][01_]*/i, Num::Bin
rule /\d[\d_]*/, Num::Integer

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

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

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

state :dotted do
pyrmont marked this conversation as resolved.
Show resolved Hide resolved
rule /\s+/m, Text
rule /[.]/, Punctuation
rule id, Name, :pop!
rule /[({\[]/, Punctuation, :pop!
end
end
end
end
18 changes: 18 additions & 0 deletions spec/lexers/lustre_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*- #

describe Rouge::Lexers::Lustre do
let(:subject) { Rouge::Lexers::Lustre.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.lus'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/x-lustre'
end
end
end

18 changes: 18 additions & 0 deletions spec/lexers/lutin_step.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*- #

describe Rouge::Lexers::Lutin do
let(:subject) { Rouge::Lexers::Lutin.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.lut'
end

it 'guesses by mimetype' do
assert_guess :mimetype => 'text/x-lutin'
end
end
end

Loading