Syntax Manual :: Chapter 1: Introduction

Syntax— Lexical Analysis for Syntax Highlighting | Syntax Version: 1.0.0 Manual Last Updated: 2005-06-18 20:25 UTC

| Up | Next (2. Lexical Analysis) # 1. Introduction ## 1.1. What is Syntax? Syntax is, first and foremost, a lexical analysis framework. It supports pluggable syntax modules, and comes with modules for Ruby, XML, and YAML. What does it mean, a “lexical analysis framework”? It means you can use Syntax to take a body of text representing instructions in some syntax (like Ruby), and break that text into tokens. What is that good for? Well, a few things. You could concievably use it to count the number of classes in a system, or the number of lines of code per method. But Syntax was written specifically with syntax highlighting in mind. ## 1.2. Quick Start Because Syntax was written primarily to support syntax highlighting, that is going to be the task most clients of the library will use it for. So, to get you up and running quickly, converting, for example, Ruby code to HTML, consider the following: Convert a Ruby script to HTML [ruby]
1 2 3 4 5 6 |
    require 'syntax/convertors/html'

    convertor = Syntax::Convertors::HTML.for_syntax "ruby"
    html = convertor.convert( File.read( "program.rb" ) )

    puts html
The above script will convert a Ruby file called `program.rb` to HTML, writing the HTML to stdout. HTML `span` tags will be used in conjunction with “class” attributes to describe the various tokens—this allows you to use CSS to colorize the result. Incidentally, this entire document uses the Syntax library to colorize the Ruby code snippets! Note: currently, only HTML conversion is supported, but it should be possible to create a convertor that takes the tokenized output and emits other formats, like LaTeX, PDF, or Rich Text. Up | Next (2. Lexical Analysis)