<commit_before>#!/usr/bin/env ruby

class GlyphSyntaxNode < Treetop::Runtime::SyntaxNode 

	attr_reader :hashnode

	def evaluate(context, current=nil)
		current ||= context.to_node
		@hashnode ||= current.to_node
		value = elements.map { |e| e.evaluate(context, current) if e.respond_to? :evaluate }.join 
		escs = [
			['\\]', ']'], 
			['\\[', '['],
			['\\=', '=']
		]
		escs.each{|e| value.gsub! e[0], e[1]}
		value
	end

end 


class MacroNode < GlyphSyntaxNode

	def evaluate(context, current=nil)
		name = macro_name.text_value.to_sym
		raise RuntimeError, "Undefined macro '#{name}'" unless Glyph::MACROS.include? name
		@hashnode = {:macro => name, :source => context[:source]}.to_node
		current << @hashnode
		value = super(context, @hashnode).strip 
		@hashnode[:value] = value
		Glyph::MACROS[name].call(@hashnode).to_s
	end

end


class TextNode < GlyphSyntaxNode	

	def evaluate(context, current=nil)
		text_value
	end

end


module Glyph

	module Preprocessor

		extend Actions

		PARSER = ::GlyphLanguageParser.new

		#def self.process(text, context={})
		#	analyze(text, context)[:output]
		#end

		def self.process(text, context={})
			begin
				node = PARSER.parse(text)
				context[:source] ||= "--"
				text = node.evaluate(context, nil)
				node.hashnode[:output] = text
				node.hashnode
			rescue Exception => e
				raise if e.is_a? MacroError
				if Glyph.testing? then
					raise 
				else
					raise RuntimeError, "An error occurred when preprocessing #{context[:source]}"
				end
			end
		end

		def self.build_document
			context = {:soure => "file: document.glyph"}
			Glyph::DOCUMENT.from process(file_load(Glyph::PROJECT/'document.glyph'), context)
		end

		def self.macro(name, &block)
			Glyph::MACROS[name.to_sym] = block			
		end<commit_msg>Minor corrections.<commit_after>#!/usr/bin/env ruby

class GlyphSyntaxNode < Treetop::Runtime::SyntaxNode 

	attr_reader :hashnode

	def evaluate(context, current=nil)
		current ||= context.to_node
		@hashnode ||= current.to_node
		value = elements.map { |e| e.evaluate(context, current) if e.respond_to? :evaluate }.join 
		escs = [
			['\\]', ']'], 
			['\\[', '['],
			['\\=', '=']
		]
		escs.each{|e| value.gsub! e[0], e[1]}
		value
	end

end 


class MacroNode < GlyphSyntaxNode

	def evaluate(context, current=nil)
		name = macro_name.text_value.to_sym
		raise RuntimeError, "Undefined macro '#{name}'" unless Glyph::MACROS.include? name
		@hashnode = {:macro => name, :source => context[:source]}.to_node
		current << @hashnode
		value = super(context, @hashnode).strip 
		@hashnode[:value] = value
		Glyph::MACROS[name].call(@hashnode).to_s
	end

end


class TextNode < GlyphSyntaxNode	

	def evaluate(context, current=nil)
		text_value
	end

end


module Glyph

	module Preprocessor

		extend Actions

		PARSER = ::GlyphLanguageParser.new

		def self.process(text, context={})
			begin
				node = PARSER.parse(text)
				context[:source] ||= "--"
				text = node.evaluate(context, nil)
				node.hashnode[:output] = text
				node.hashnode
			rescue Exception => e
				raise if e.is_a? MacroError
				if Glyph.testing? then
					raise 
				else
					raise RuntimeError, "An error occurred when preprocessing #{context[:source]}"
				end
			end
		end

		def self.build_document
			context = {:source => "file: document.glyph"}
			Glyph::DOCUMENT.from process(file_load(Glyph::PROJECT/'document.glyph'), context)
		end

		def self.macro(name, &block)
			Glyph::MACROS[name.to_sym] = block			
		end
{'fraction_non_alphanumeric': 0.1434732604945371, 'fraction_numerical': 0.0011500862564692352, 'mean_word_length': 3.7013513513513514, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3796240', 'n_tokens_mistral': 1428, 'n_tokens_neox': 1209, 'n_words': 268}