Skip to content

Commit

Permalink
Add PostScript Lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
taasan committed Feb 21, 2019
1 parent e957c74 commit 6e48606
Show file tree
Hide file tree
Showing 3 changed files with 643 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/rouge/demos/postscript
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
%!PS
/Courier % name the desired font
20 selectfont % choose the size in points and establish
% the font as the current one
72 500 moveto % position the current point at
% coordinates 72, 500 (the origin is at the
% lower-left corner of the page)
(Hello world!) show % stroke the text in parentheses
showpage % print all on the page
97 changes: 97 additions & 0 deletions lib/rouge/lexers/postscript.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

# Adapted from pygments PostScriptLexer
module Rouge
module Lexers
class PostScript < RegexLexer
"""
Lexer for PostScript files.
The PostScript Language Reference published by Adobe at
<http://partners.adobe.com/public/developer/en/ps/PLRM.pdf>
is the authority for this.
"""

title "PostScript"
desc "PostScript"
tag "postscript"
aliases "postscr", "postscript"
filenames "*.ps", "*.eps"
mimetypes "application/postscript"

delimiter = %s"()<>\[\]{}/%\s"
delimiter_end = Regexp.new("(?=[#{delimiter}])")
valid_name_chars = Regexp.new("[^#{delimiter}]")
valid_name = /#{valid_name_chars}+#{delimiter_end}/

# These keywords taken from
# <http://www.math.ubc.ca/~cass/graphics/manual/pdf/a1.pdf>
# Is there an authoritative list anywhere that doesn't involve
# trawling documentation?
keywords = %w/abs add aload arc arcn array atan begin
bind ceiling charpath clip closepath concat
concatmatrix copy cos currentlinewidth currentmatrix
currentpoint curveto cvi cvs def defaultmatrix
dict dictstackoverflow div dtransform dup end
exch exec exit exp fill findfont floor get
getinterval grestore gsave gt identmatrix idiv
idtransform index invertmatrix itransform length
lineto ln load log loop matrix mod moveto
mul neg newpath pathforall pathbbox pop print
pstack put quit rand rangecheck rcurveto repeat
restore rlineto rmoveto roll rotate round run
save scale scalefont setdash setfont setgray
setlinecap setlinejoin setlinewidth setmatrix
setrgbcolor shfill show showpage sin sqrt
stack stringwidth stroke strokepath sub syntaxerror
transform translate truncate typecheck undefined
undefinedfilename undefinedresult/

state :root do
# All comment types
rule %r'^%!.+?$', Comment::Preproc
rule %r'%%.*?$', Comment::Special
rule %r'(^%.*?$){2,}', Comment::Multiline
rule %r'%.*?$', Comment::Single

# String literals are awkward; enter separate state.
rule %r'\(', Str, :stringliteral

# References
rule %r'/#{valid_name}', Name::Variable

rule %r'[{}<>\[\]]', Punctuation

rule %r'(?:#{keywords.join('|')}#{delimiter_end})', Name::Builtin

# Conditionals / flow control
rule /(eq|ne|g[et]|l[et]|and|or|not|if(?:else)?|for(?:all)?)#{delimiter_end}/, Keyword::Reserved
rule /(false|true)#{delimiter_end}/, Keyword::Constant

# Numbers
rule %r'<[0-9A-Fa-f]+>#{delimiter_end}', Num::Hex
# Slight abuse: use Oct to signify any explicit base system
rule %r'[0-9]+\#(\-|\+)?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)((e|E)[0-9]+)?#{delimiter_end}', Num::Oct
rule %r'(\-|\+)?([0-9]+\.?|[0-9]*\.[0-9]+|[0-9]+\.[0-9]*)((e|E)[0-9]+)?#{delimiter_end}', Num::Float
rule %r'(\-|\+)?[0-9]+#{delimiter_end}', Num::Integer

# Names
rule valid_name, Name::Function # Anything else is executed

rule /\s+/, Text
end

state :stringliteral do
rule %r'[^()\\]+', Str
rule %r'\\', Str::Escape, :escape
rule %r'\(', Str, :stringliteral
rule %r'\)', Str, :pop!
end

state :escape do
rule /[0-8]{3}|n|r|t|b|f|\\|\(|\)/, Str::Escape, :pop!
end
end
end
end
Loading

0 comments on commit 6e48606

Please sign in to comment.