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

Added a simple reprap G-Code parser. #789

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/rouge/demos/gcode
@@ -0,0 +1,28 @@
; generated by Slic3r 1.3.0-dev on 2016-07-07 at 16:40:29

M104 S60 ; set temperature
G28 ; home all X Y e Z
G21 ;metric values
G90 ;absolute positioning
M107 ;start with the fan off
G1 Z2 F210
G92 E0 ;zero the extruded length
M117 MakerLinux S.A.
M109 S60 ; set temperature and wait for it to be reached
G21 ; set units to millimeters
G90 ; use absolute coordinates
M82 ; use absolute distances for extrusion
G92 E0
G0 Z0.700 F4800.000
G1 X67.159 Y91.922 F4800.000
G1 F600
G1 X69.217 Y90.533 E0.69133
G1 X70.335 Y90.170 E1.01876
G1 X71.706 Y89.949 E1.40553
G1 X131.985 Y89.693 E18.19146
G92 E0
M104 S0 ; turn off temperature
G28 X0 ; home X axis
M84 ; disable motors
M140 S0 ; turn off heated bed
; filament used = 5172.5mm (16.2cm3)
28 changes: 28 additions & 0 deletions lib/rouge/lexers/gcode.rb
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*- #

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add '# frozen_string_literal: true'

module Rouge
module Lexers
class GCode < RegexLexer
tag 'gcode'
aliases 'g-code', 'ngc'

title "G-Code"
desc 'A generic lexer for reprap/cnc g-code files'
filenames '*.gco', '*.ngc', '*.gcode', '*.g'

# short and sweet
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed short and sweet, but please remove

state :root do
rule /^N[0-9][0-9]*\s*/, Comment::Special
rule /\;.*?\n/, Comment
rule /\*[0-9][0-9]*\s*\;?/, Comment::Doc
rule /^M117.*\n/, Keyword::Declaration
rule /\s\s*/, Text::Whitespace
rule /^G[0-9.][0-9.]*/, Keyword::Namespace
rule /^M[0-9.][0-9.]*/, Keyword::Constant
rule /[PSRTXYZJDHQW][+-]?[0-9.]*/, Keyword::Type
rule /E[+-]?[0-9.]*/, Name::Variable
rule /F[+-]?[0-9.]*/, Literal::String::Char
end
end
end
end
13 changes: 13 additions & 0 deletions spec/lexers/gcode_spec.rb
@@ -0,0 +1,13 @@
# -*- coding: utf-8 -*- #

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add '# frozen_string_literal: true'

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

describe 'guessing' do
include Support::Guessing

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