node/lib/metasm/metasm/parse.rb
# parses an asm source file to an array of Instruction/Data/Align/Offset/Padding
parses an asm source file to an array of Instruction/Data/Align/Offset/Padding
parses an asm source file to an array of Instruction / Data / Align / Offset / Padding
def parse(text, file='<ruby>', lineno=0)
		parse_init
		@lexer ||= cpu.new_asmprepro('', self)
		@lexer.feed text, file, lineno
		lasteol = true
		while not @lexer.eos?
			tok = @lexer.readtok
			next if not tok
			case tok.type
			when :space
			when :eol
				lasteol = true
			when :punct
				case tok.raw
				when '.'
					tok = tok.dup
					while ntok = @lexer.nexttok and ((ntok.type == :string) or (ntok.type == :punct and ntok.raw == '.'))
						tok.raw << @lexer.readtok.raw
					end
					parse_parser_instruction tok
				else raise tok, 'syntax error'
				end
				lasteol = false
			when :string
				ntok = nntok = nil
				if lasteol and ((ntok = @lexer.readtok and ntok.type == :punct and ntok.raw == ':') or
						(ntok and ntok.type == :space and nntok = @lexer.nexttok and nntok.type == :string and Data::DataSpec.include?(nntok.raw)))
					if tok.raw =~ /^[1-9][0-9]*$/
						lname = @locallabels_bkw[tok.raw] = @locallabels_fwd.delete(tok.raw) || new_label('local_'+tok.raw)
					else
						lname = tok.raw
						raise tok, "invalid label name: #{lname.inspect} is reserved" if @cpu.check_reserved_name(lname)
						raise tok, "label redefinition" if new_label(lname) != lname
					end
					l = Label.new(lname)
					l.backtrace = tok.backtrace
					@cursource << l
					lasteol = false
				else
					lasteol = false
					@lexer.unreadtok ntok
					@lexer.unreadtok tok
					if Data::DataSpec.include?(tok.raw)
						@cursource << parse_data
					else
						@cursource << @cpu.parse_instruction(@lexer)
					end
					if lname = @locallabels_fwd.delete('endinstr')
						l = Label.new(lname)
						l.backtrace = tok.backtrace
						@cursource << l
					end
				end
			else
				raise tok, 'syntax error'
			end
		end
		puts "Undefined forward reference to anonymous labels #{@locallabels_fwd.keys.inspect}" if $VERBOSE and not @locallabels_fwd.empty?
		self
	end
def parse ( text , file = '<ruby>' , lineno = 0 ) parse_init @lexer ||= cpu . new_asmprepro ( '' , self ) @lexer . feed text , file , lineno lasteol = true while not @lexer . eos? tok = @lexer . readtok next if not tok case tok . type when :space when :eol lasteol = true when :punct case tok . raw when '.' tok = tok . dup while ntok = @lexer . nexttok and ( ( ntok . type == :string ) or ( ntok . type == :punct and ntok . raw == '.' ) ) tok . raw << @lexer . readtok . raw end parse_parser_instruction tok else raise tok , 'syntax error' end lasteol = false when :string ntok = nntok = nil if lasteol and ( ( ntok = @lexer . readtok and ntok . type == :punct and ntok . raw == ':' ) or ( ntok and ntok . type == :space and nntok = @lexer . nexttok and nntok . type == :string and Data :: DataSpec . include? ( nntok . raw ) ) ) if tok . raw =~ / ^[1-9][0-9]*$ / lname = @locallabels_bkw [ tok . raw ] = @locallabels_fwd . delete ( tok . raw ) || new_label ( 'local_' + tok . raw ) else lname = tok . raw raise tok , "invalid label name: #{lname.inspect} is reserved" if @cpu . check_reserved_name ( lname ) raise tok , "label redefinition" if new_label ( lname ) != lname end l = Label . new ( lname ) l . backtrace = tok . backtrace @cursource << l lasteol = false else lasteol = false @lexer . unreadtok ntok @lexer . unreadtok tok if Data :: DataSpec . include? ( tok . raw ) @cursource << parse_data else @cursource << @cpu . parse_instruction ( @lexer ) end if lname = @locallabels_fwd . delete ( 'endinstr' ) l = Label . new ( lname ) l . backtrace = tok . backtrace @cursource << l end end else raise tok , 'syntax error' end end puts "Undefined forward reference to anonymous labels #{@locallabels_fwd.keys.inspect}" if $VERBOSE and not @locallabels_fwd . empty? self end
parses an asm source file to an array of Instruction/Data/Align/Offset/Padding
parses an asm source file to an array of Instruction / Data / Align / Offset / Padding
# handle anonymous local labels
text
file
lineno