genrep5 removed, was only there to test nfa2 (now unused)

refs #15

/*
 * @LANG: c
 * @ENABLED: false
 */

#include <stddef.h>  /* NULL */
#include <stdint.h>  /* uint64_t */
#include <stdlib.h>  /* malloc(3) free(3) */
#include <stdbool.h> /* bool */
#include <string.h>
#include <stdio.h>

struct nfa_bp_rec
{
	long state;
	const unsigned char *p;
	int pop;
};

struct nfa_bp_rec nfa_bp[1024];
long nfa_len = 0;
long nfa_count = 0;

long c;

struct nfa_state_rec
{
	long c;
};

struct nfa_state_rec nfa_s[1024];

void nfa_push()
{
	nfa_s[nfa_len].c = c;
}

void nfa_pop()
{
	c = nfa_s[nfa_len].c;
}

%%{
	machine match_any;
	alphtype unsigned char;

	action eol { p+1 == eof }

	eol = '' %when eol;

	action ini6
	{
		c = 0;
	}

	action min6
	{
		c >= 2
	}

	action max6
	{
		c < 3
	}

	action inc6
	{
		c += 1;
	}

	main :=
		( :nfa2( 0, ( 'a' @{ printf( "see a %d\n", fcurs ); }  ), ini6, min6, max6, {nfa_push();}, {nfa_pop();}, inc6 ): ) {2}
		eol
		any @{printf("----- MATCH\n");}
	;

	write data;
}%%

int test( const char *data )
{
	int cs;
	const unsigned char *p = (const unsigned char *)data;
	const unsigned char *pe = p + strlen(data) + 1;
	const unsigned char *eof = pe;

	printf( "testing: %s\n", data );

	%% write init;
	%% write exec;

	return 0;
}

int main()
{
	test( "a" );
	test( "aa" );
	test( "aaa" );
	test( "aaaa" );
	test( "aaaaa" );
	test( "aaaaaa" );
	test( "aaaaaaa" );
	test( "aaaaaaaa" );
	test( "aaaaaaaaa" );
	test( "aaaaaaaaaa" );
	test( "aaaaaaaaaaa" );

	return 0;
}

##### OUTPUT #####
testing: a
testing: aa
testing: aaa
testing: aaaa
----- MATCH
testing: aaaaa
----- MATCH
----- MATCH
testing: aaaaaa
----- MATCH
testing: aaaaaaa
testing: aaaaaaaa


setting mode

setting mode

svn path=/trunk/; revision=1084

/* -*- mode: c; -*- */
/**
 * This file is part of Slideshow.
 * Copyright (C) 2008-2010 David Sveningsson <ext@sidvind.com>
 *
 * Slideshow is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * Slideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Slideshow.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif

#include "Browser.h"
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>

#ifdef WIN32
#	include "win32.h"
#endif

#define BUFLEN 1024

struct state_t {
	char buffer[BUFLEN+1];
	int buflen;
	int cs;
};

%%{
	machine fx_parser;
	access fsm->;

	# Append to the buffer.
	action append { if ( fsm->buflen < BUFLEN ) fsm->buffer[fsm->buflen++] = fc; }
	action clear { fsm->buflen = 0; }
	action term { if ( fsm->buflen < BUFLEN ) fsm->buffer[fsm->buflen++] = 0; }

	action set_provider { ctx->provider = strdup(fsm->buffer); }
	action set_user     { ctx->user = strdup(fsm->buffer); }
	action set_pass     { ctx->pass = strdup(fsm->buffer); }
	action set_host     { ctx->host = strdup(fsm->buffer); }
	action set_src      { ctx->name = strdup(fsm->buffer); }

	name = alnum+ >clear $append %term;
	string = [^\0]+ >clear $append %term;

	provider = name %set_provider;
	user = name %set_user;
	pass = name %set_pass;
	host = name %set_host;
	src = string %set_src 0;

	main := provider '://' (
		user ':' pass '@' host '/' src |
		user '@' host '/' src |
		host '/' src |
		src # note that this pattern DOES NOT start with an initial slash, this
		    # is because that would require the user to pass an extra slash with
		    # filebacked browsers. Rather the slash is considered a parth of the
		    # path instead of as a separator.
		    #
		    # eg sqlite:///abs/path would be sqlite:////abs/path
	);
}%%

%% write data;

int parse(struct state_t* fsm, browser_context_t* ctx, const char* line){
	assert(fsm);
	assert(ctx);

	fsm->buflen = 0;

	%% write init;

	const char* p = line;
	const char* pe = line + (strlen(p) + 1);

	%% write exec;

	if ( fsm->cs == fx_parser_error ) {
		printf("parse error\n");
		return -1;
	}

	return 0;
}

#ifdef __cplusplus
extern "C" {
#endif

browser_context_t get_context(const char* string){
	struct state_t fsm;
	browser_context_t ctx;
	ctx.provider = NULL;
	ctx.user = NULL;
	ctx.pass = NULL;
	ctx.host = NULL;
	ctx.name = NULL;

//	fsm.stack = NULL;
//	fsm.stack_size = 0;

	if ( parse(&fsm, &ctx, string) != 0 ){
		fprintf(stderr, "Failed to parse browser-string\n");
	}

	return ctx;
}

void free_context(browser_context_t* context){
	free(context->provider);
	free(context->user);
	free(context->pass);
	free(context->host);
	free(context->name);
}

#ifdef __cplusplus
}
#endif


simplify

simplify

%%{
  machine params_parser;
  action H { @head = p }
  action T { @tail = p }
  action inject_key { @hash[key] = {} }
  action push { @hash_stack.push @hash; @hash = @hash[key] }
  action pop { @hash = @hash_stack.pop }

  DELIMITER = ','                                   >H@T @{ log(:delimiter) };
  LEVEL = '('+                                      >H@T @{ log(:level) };
  END_LEVEL = ')'+                                  >H@T @{ log(:end_level); };
  INPUT = (any - LEVEL - END_LEVEL - DELIMITER)+    >H@T @{ log(:input); memo_key; };

  main := (
    INPUT
    (
      DELIMITER @inject_key @{ fnext main; } |
      END_LEVEL DELIMITER? @inject_key @pop @{ fret; } |
      LEVEL @inject_key @push @{ fcall main; }
    )
  )*;
}%%

module ParamsTree
  class Parser
    attr_accessor :data

    def initialize
      %% write data;
    end

    def process(input)
      @data = input.unpack('c*')
      stack = []

      @log = []
      @hash = {}
      @hash_stack = []
      @key = nil

      %% write init;
      %% write exec;

      #@hash.inspect
      #@hash_stack.inspect
      @hash_stack.first
    end

    def chars
      @data[@head..@tail]
    end

    def strokes
      chars.pack('c*')
    end

    def log(token)
      @log << { token => strokes }
    end

    def memo_key
      @key = chars
    end

    def key
      @key.pack('c*')
    end
  end
end

s = "default(all,usadasdid(zczxc(pio(xqwe)),cvb)),isdsdd(wow,er),sed(rew,tre,yrt(dfg,gfd))"
puts s
puts ParamsTree::Parser.new.process s

%%{
  machine params_parser;
  action H { @head = p }
  action T { @tail = p }
  action inject_key { @hash[key] = {} }
  action push { @hash_stack.push @hash; @hash = @hash[key] }
  action pop { @hash = @hash_stack.pop }

  DELIMITER = ','                                   >H@T @{ log(:delimiter) };
  LEVEL = '('+                                      >H@T @{ log(:level) };
  END_LEVEL = ')'+                                  >H@T @{ log(:end_level); };
  INPUT = (any - LEVEL - END_LEVEL - DELIMITER)+    >H@T @{ log(:input); memo_key; };

  main := (
    INPUT
    (
      DELIMITER @inject_key @{ fnext main; } |
      END_LEVEL DELIMITER? @inject_key @pop @{ fret; } |
      LEVEL @inject_key @push @{ fcall main; }
    )
  )*;
}%%

module ParamsTree
  class Parser
    attr_accessor :data

    def initialize
      %% write data;
    end

    def process(input)
      @data = input.unpack('c*')
      stack = []

      @log = []
      @hash = {}
      @hash_stack = []
      @key = nil

      %% write init;
      %% write exec;

      #@hash.inspect
      #@hash_stack.inspect
      @hash_stack.first
    end

    def char
      @data[@head..@tail]
    end

    def strokes
      char.pack('c*')
    end

    def log(token)
      @log << { token => strokes }
    end

    def memo_key
      @key = char
    end

    def key
      @key.pack('c*')
    end
  end
end

s = "default(all,usadasdid(zczxc(pio(xqwe)),cvb)),isdsdd(wow,er),sed(rew,tre,yrt(dfg,gfd))"
puts s
puts ParamsTree::Parser.new.process s


Handle compound FASTA characters

Handle compound FASTA characters

– The nucleic acid codes are treated as distinct characters for now.


Moved calling of inc/dec actions outside cs_string scanner machine to make it easier to read in Rebol Scanner.

package net.ohloh.ohcount4j.scan;

import net.ohloh.ohcount4j.Language;

public class RebolScanner extends BaseScanner{

	protected int nested_level = 0;

  	%%{
	    machine rebol;
	    include common "common.rl";

	    action inc_nested_level { nested_level++; }
	 	action dec_nested_level { nested_level--; if(nested_level == 0) fret; }
	  	action is_nested { nested_level > 0 }

	    rebol_line_comment = ';' @comment nonnewline* @comment;

	    # For handeling nested nature of bracket strings { }
	    rebol_cb_string_begin = '{' @code @inc_nested_level;
	    rebol_cb_string_end = '}' @code when is_nested @dec_nested_level;
	    rebol_cb_string := |*
	   		newline;
	   		spaces;
	   		(any - newline) => code;
	    	rebol_cb_string_begin;
	    	rebol_cb_string_end;
	    *|;

	  	rebol_line := |*
	  		rebol_cb_string_begin => { fcall rebol_cb_string; };
	  		string_literal_dq;
	    	rebol_line_comment;
	    	spaces;
	    	newline;
	    	(any - newline) => code;
	  	*|;
	}%%

	%% write data;

	@Override
	public void doScan(){
		// variables and data is set up in BaseScanner
	    %% write init;
	    init();
	    %% write exec;
	}

	@Override
	public Language getLanguage() {
		return Language.LANG_REBOL;
	}

}

package net.ohloh.ohcount4j.scan;

import net.ohloh.ohcount4j.Language;

public class RebolScanner extends BaseScanner{

	protected int str_nested_level = 0;

  	%%{
	    machine rebol;
	    include common "common.rl";

	    action inc_str_nested_level { str_nested_level++; }
	 	action dec_str_nested_level { str_nested_level--; if(str_nested_level == 0) fret; }
	  	action is_nested { str_nested_level > 0 }

	    rebol_line_comment = ';' @comment nonnewline* @comment;

	    # Anything between curly brackets { } is considered a string
	    # Brackets may be nested so string does not end until matching bracket is found
	    # If not enough terminating brackets, rest of file is considered part of the string
	    rebol_cb_string_begin = '{' @code;
	    rebol_cb_string_end = '}' @code;
	    rebol_cb_string := |*
	   		newline;
	   		spaces;
	   		(any - newline) => code;
	    	rebol_cb_string_begin @inc_str_nested_level;
	    	rebol_cb_string_end when is_nested @dec_str_nested_level;
	    *|;

	  	rebol_line := |*
	  		rebol_cb_string_begin => { fhold; fcall rebol_cb_string; };
	  		string_literal_dq;
	    	rebol_line_comment;
	    	spaces;
	    	newline;
	    	(any - newline) => code;
	  	*|;
	}%%

	%% write data;

	@Override
	public void doScan(){
		// variables and data is set up in BaseScanner
	    %% write init;
	    init();
	    %% write exec;
	}

	@Override
	public Language getLanguage() {
		return Language.LANG_REBOL;
	}

}