# Syntax Highlighting (#218)

**URL:** <https://www.ruby-forum.com/t/syntax-highlighting-218/174264>\
**Category:** Ruby\
**Created:** [August 24, 2009, 2:57am UTC](https://www.ruby-forum.com/t/syntax-highlighting-218/174264 "2009-08-24T02:57:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 24, 2009, 2:57am UTC](https://www.ruby-forum.com/t/syntax-highlighting-218/174264/1 "2009-08-24T02:57:22Z")

</div>

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

The three rules of Ruby Q.:

1. Please do not post any solutions or spoiler discussion for this  
quiz until 48 hours have elapsed from the time this message was  
sent.

2. Support Ruby Q. by submitting ideas and responses  
as often as you can!  
Visit: [http://rubyquiz.strd6.com/suggestions](http://rubyquiz.strd6.com/suggestions)

3. Enjoy!

Suggestion: A [QUIZ] in the subject of emails about the problem  
helps everyone on Ruby T. follow the discussion. Please reply to  
the original quiz message, if you can.

RSS Feed: [http://rubyquiz.strd6.com/quizzes.rss](http://rubyquiz.strd6.com/quizzes.rss)

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

## Syntax Highlighting (# 218)

Namo namah Rubyists,

This week’s quiz is to write a syntax highlighter. Your program or  
method will take as input unadorned Ruby code and return marked-up  
code. Different syntactical elements of the code should have different  
styles. As an additional challenge you may wish to indicate syntax  
errors at the point in which they occur in the code. You may choose  
any output style that you like. If you are unsure of what to use to  
colorize output, then check out Term::ANSIColor[1](http://flori.github.com/term-ansicolor/).

Have Fun!

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [August 26, 2009, 5:50pm UTC](https://www.ruby-forum.com/t/syntax-highlighting-218/174264/2 "2009-08-26T17:50:20Z")

</div>

On 8/23/09, Daniel M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This week’s quiz is to write a syntax highlighter. Your program or  
> method will take as input unadorned Ruby code and return marked-up  
> code. Different syntactical elements of the code should have different  
> styles. As an additional challenge you may wish to indicate syntax  
> errors at the point in which they occur in the code. You may choose  
> any output style that you like. If you are unsure of what to use to  
> colorize output, then check out Term::ANSIColor[1].

This was a fun little quiz. Of course, I cheated like mad. (I think  
that was the point, tho.) My entry uses RubyLexer, my own stand-alone  
lexer for ruby. It’ll be interesting to see what other submissions  
look like.

---

<div class="post-metadata">

**Author:** ![danmcb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/13edae/32.png) [@danmcb](https://www.ruby-forum.com/u/danmcb)\
**Post date:** [September 8, 2009, 12:39am UTC](https://www.ruby-forum.com/t/syntax-highlighting-218/174264/3 "2009-09-08T00:39:50Z")

</div>

This week’s quiz was solved by Caleb C…

Caleb used the `RubyLexer` gem to parse a Ruby file given as a command  
line argument. `Term::ANSIColor` is used to color the tokens.

```
def coloruby file,fd=open(file)
  lexer=RubyLexer.new(file,fd)
  begin
    token=lexer.get1token
    print token.colorize
  end until RubyLexer::EoiToken===token
ensure
  print Term::ANSIColor.reset
end

```

The `coloruby` method uses `RubyLexer` to generate a stream of tokens.  
Each token is then colorized and printed out. After all the tokens are  
printed the color is reset with `Term::ANSIColor.reset` so that any  
following text won’t receive collateral colorization.

So how are these tokens colorized anyway? Caleb’s solution opens up  
the `RubyLexer` class and adds a `colorize` method to all tokens.  
`Term::ANSIColor` is also included in the `Token` class so that all  
the color methods are available as well.

```
class Token
  include Term::ANSIColor

  def colorize
    color+ident.to_s
  end
end

```

Each individual token class may define its own different color or  
colorize method.

```
class MethNameToken
  alias color green
end

class KeywordToken
  def colorize
    if /[^a-z]/i===ident
      yellow+ident
    else
      red+ident
    end
  end
end

```

The end result is nice colorful Ruby code.[1](http://rubyquiz.strd6.com/quizzes/218/colorized.png)

Thank you Caleb for your solution to this week’s quiz!

[Syntax Highlighting (#218) - Solutions](http://rubyquiz.strd6.com/quizzes/218.tar.gz)
