-
Notifications
You must be signed in to change notification settings - Fork 740
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds support for the Brainfuck language.
- Loading branch information
1 parent
de91c27
commit 004b6f4
Showing
4 changed files
with
105 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[ | ||
This is a sample | ||
comment. | ||
] | ||
.[>+<-]>, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
module Rouge | ||
module Lexers | ||
class Brainfuck < RegexLexer | ||
tag 'brainfuck' | ||
filenames '*.b', '*.bf' | ||
mimetypes 'text/x-brainfuck' | ||
|
||
title "Brainfuck" | ||
desc "The Brainfuck programming language" | ||
|
||
start { push :bol } | ||
|
||
state :bol do | ||
rule /\s+/m, Text | ||
rule /\[/, Comment::Multiline, :comment_multi | ||
rule(//) { pop! } | ||
end | ||
|
||
state :root do | ||
rule /\]/, Error | ||
rule /\[/, Punctuation, :loop | ||
|
||
mixin :comment_single | ||
mixin :commands | ||
end | ||
|
||
state :comment_multi do | ||
rule /\[/, Comment::Multiline, :comment_multi | ||
rule /\]/, Comment::Multiline, :pop! | ||
rule /[^\[\]]+?/m, Comment::Multiline | ||
end | ||
|
||
state :comment_single do | ||
rule /[^><+\-.,\[\]]+/, Comment::Single | ||
end | ||
|
||
state :loop do | ||
rule /\[/, Punctuation, :loop | ||
rule /\]/, Punctuation, :pop! | ||
mixin :comment_single | ||
mixin :commands | ||
end | ||
|
||
state :commands do | ||
rule /[><]+/, Name::Builtin | ||
rule /[+\-.,]+/, Name::Function | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# -*- coding: utf-8 -*- # | ||
# frozen_string_literal: true | ||
|
||
describe Rouge::Lexers::Brainfuck do | ||
let(:subject) { Rouge::Lexers::Brainfuck.new } | ||
|
||
describe 'guessing' do | ||
include Support::Guessing | ||
|
||
it 'guesses by filename' do | ||
assert_guess :filename => 'foo.b' | ||
assert_guess :filename => 'FOO.bf', :source => 'foo' | ||
end | ||
|
||
it 'guesses by mimetype' do | ||
assert_guess :mimetype => 'text/x-brainfuck' | ||
end | ||
end | ||
|
||
describe 'lexing' do | ||
include Support::Lexing | ||
|
||
it 'recognizes one-line comments not followed by a newline (#796)' do | ||
assert_tokens_equal 'comment', ['Comment.Single', 'comment'] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
This program will print "Hello world!" | ||
] | ||
++++++++++ | ||
[ | ||
>+++++++>++++++++++>+++>+<<<<- | ||
] | ||
>++. Initial loop (it will be printed an H) | ||
>+. e | ||
+++++++. l | ||
. l | ||
+++. o | ||
>++. space | ||
<<+++++++++++++++. W | ||
>. o | ||
+++. r | ||
------. l | ||
--------. d | ||
>+. ! | ||
>. |