Skip to content

Instantly share code, notes, and snippets.

@yorickpeterse
Last active August 29, 2015 13:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yorickpeterse/10658884 to your computer and use it in GitHub Desktop.
Save yorickpeterse/10658884 to your computer and use it in GitHub Desktop.
Basic boilerplate for Ragel and Ruby, extracted from Oga.
# Since public domain doesn't exist everywhere in the world and people might
# actually want to use this snippet here's the license (you can leave out
# the lines preceding it):
#
# Copyright (c) 2014, Yorick Peterse
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
%%machine lexer; # %
module MyProject
class Lexer
%% write data; # %
# Lazy way of forwarding instance method calls used internally by Ragel
# to their corresponding class methods.
private_methods.grep(/^_lexer_/).each do |name|
define_method(name) do
return self.class.send(name)
end
private(name)
end
##
# @param [String] data The data to lex.
#
def initialize(data)
@data = data.unpack('U*')
reset
end
##
# Resets the internal state of the lexer. Typically you don't need to
# call this method yourself as its called by #lex after lexing a given
# String.
#
def reset
@line = 1
@ts = nil
@te = nil
@tokens = []
@stack = []
@top = 0
@cs = self.class.lexer_start
@act = 0
@eof = @data.length
@p = 0
@pe = @eof
end
##
# Lexes the supplied String and returns an Array of tokens. Each token is
# an Array in the following format:
#
# [TYPE, VALUE]
#
# The type is a symbol, the value is either nil or a String.
#
# This method resets the internal state of the lexer after consuming the
# input.
#
# @param [String] data The string to consume.
# @return [Array]
# @see #advance
#
def lex
tokens = []
while token = advance
tokens << token
end
reset
return tokens
end
##
# Advances through the input and generates the corresponding tokens.
#
# This method does *not* reset the internal state of the lexer.
#
# @param [String] data The String to consume.
# @return [Array]
#
def advance
%% write exec; # % fix highlight
return @tokens.shift
end
private
##
# @param [Fixnum] amount The amount of lines to advance.
#
def advance_line(amount = 1)
@line += amount
end
##
# Emits a token who's value is based on the supplied start/stop position.
#
# @param [Symbol] type The token type.
# @param [Fixnum] start
# @param [Fixnum] stop
#
# @see #text
# @see #add_token
#
def emit(type, start = @ts, stop = @te)
value = text(start, stop)
add_token(type, value)
end
##
# Returns the text of the current buffer based on the supplied start and
# stop position.
#
# By default `@ts` and `@te` are used as the start/stop position.
#
# @param [Fixnum] start
# @param [Fixnum] stop
# @return [String]
#
def text(start = @ts, stop = @te)
return @data[start...stop].pack('U*')
end
##
# Adds a token with the given type and value to the list.
#
# @param [Symbol] type The token type.
# @param [String] value The token value.
#
def add_token(type, value = nil)
token = [type, value, @line]
@tokens << token
end
%%{
# Use instance variables for `ts` and friends.
access @;
getkey (@data[@p] || 0);
variable p @p;
variable pe @pe;
variable eof @eof;
newline = '\n' | '\r\n';
whitespace = [ \t];
main := |*
# Note that this rule should be declared at the very bottom as it
# will otherwise take precedence over the other rules.
any => {
};
*|;
}%%
end # Lexer
end # MyProject
@yorickpeterse
Copy link
Author

Note that I honestly don't give a crap about the license for this. You can dump it wherever you like or simply not include it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment