2018-01-12 03:38:09 -05:00
|
|
|
#! /your/favourite/path/to/ruby
|
2018-12-27 01:12:09 -05:00
|
|
|
# -*- Ruby -*-
|
2018-01-12 03:38:09 -05:00
|
|
|
# -*- frozen_string_literal: true; -*-
|
|
|
|
# -*- warn_indent: true; -*-
|
|
|
|
#
|
|
|
|
# Copyright (c) 2017 Urabe, Shyouhei. All rights reserved.
|
|
|
|
#
|
|
|
|
# This file is a part of the programming language Ruby. Permission is hereby
|
|
|
|
# granted, to either redistribute and/or modify this file, provided that the
|
|
|
|
# conditions mentioned in the file COPYING are met. Consult the file for
|
|
|
|
# details.
|
|
|
|
|
|
|
|
require 'pathname'
|
|
|
|
|
2018-01-12 03:38:11 -05:00
|
|
|
# Poor man's StringScanner.
|
|
|
|
# Sadly https://bugs.ruby-lang.org/issues/8343 is not backported to 2.0. We
|
|
|
|
# have to do it by hand.
|
2018-01-12 03:38:09 -05:00
|
|
|
class RubyVM::Scanner
|
|
|
|
attr_reader :__FILE__
|
|
|
|
attr_reader :__LINE__
|
|
|
|
|
|
|
|
def initialize path
|
2018-10-18 19:58:21 -04:00
|
|
|
src = Pathname.new(__FILE__).relative_path_from(Pathname.pwd).dirname
|
2018-01-12 03:38:09 -05:00
|
|
|
src += path
|
|
|
|
@__LINE__ = 1
|
2018-10-18 19:58:21 -04:00
|
|
|
@__FILE__ = src.to_path
|
2018-01-12 03:38:11 -05:00
|
|
|
@str = src.read mode: 'rt:utf-8:utf-8'
|
|
|
|
@pos = 0
|
2018-01-12 03:38:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def eos?
|
2018-01-12 03:38:11 -05:00
|
|
|
return @pos >= @str.length
|
2018-01-12 03:38:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def scan re
|
|
|
|
ret = @__LINE__
|
2018-01-12 03:38:11 -05:00
|
|
|
@last_match = @str.match re, @pos
|
|
|
|
return unless @last_match
|
|
|
|
@__LINE__ += @last_match.to_s.count "\n"
|
|
|
|
@pos = @last_match.end 0
|
2018-01-12 03:38:09 -05:00
|
|
|
return ret
|
|
|
|
end
|
|
|
|
|
|
|
|
def scan! re
|
|
|
|
scan re or raise sprintf "parse error at %s:%d near:\n %s...", \
|
2018-09-20 01:31:51 -04:00
|
|
|
@__FILE__, @__LINE__, @str[@pos, 32]
|
2018-01-12 03:38:09 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def [] key
|
2018-01-12 03:38:11 -05:00
|
|
|
return @last_match[key]
|
2018-01-12 03:38:09 -05:00
|
|
|
end
|
|
|
|
end
|