Skip to content

Commit

Permalink
Merge remote branch 'ghnatiuk/respect-for-whitespace-in-descriptions'…
Browse files Browse the repository at this point in the history
… into respect-for-whitespace-in-descriptions-java
  • Loading branch information
aslakhellesoy committed Dec 5, 2010
2 parents 415cfba + 5120539 commit da50e80
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 52 deletions.
4 changes: 2 additions & 2 deletions lib/gherkin/formatter/pretty_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def examples(examples)
print_comments(examples.comments, ' ')
print_tags(examples.tags, ' ')
@io.puts " #{examples.keyword}: #{examples.name}"
print_description(examples.description, ' ')
print_description(examples.description, ' ')
table(examples.rows)
end

Expand Down Expand Up @@ -231,4 +231,4 @@ def print_indented_step_location(location)
end
end
end
end
end
68 changes: 18 additions & 50 deletions ragel/lexer.c.rl.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@
rb_str_new(ptr, len)
#endif

#define LF_FLAG 0
#define CRLF_FLAG 1
#define LF "\n"
#define CRLF "\r\n"

#ifndef RSTRING_PTR
#define RSTRING_PTR(s) (RSTRING(s)->ptr)
#endif
Expand All @@ -44,7 +39,6 @@ typedef struct lexer_state {
int line_number;
int current_line;
int start_col;
int eol;
size_t mark;
size_t keyword_start;
size_t keyword_end;
Expand All @@ -70,7 +64,7 @@ static VALUE rb_eGherkinLexingError;
store_multiline_kw_con(listener, # EVENT, \
PTR_TO(keyword_start), LEN(keyword_start, PTR_TO(keyword_end - 1)), \
PTR_TO(content_start), LEN(content_start, PTR_TO(content_end)), \
lexer->current_line, lexer->eol); \
lexer->current_line, lexer->start_col); \
if (lexer->content_end != 0) { \
p = PTR_TO(content_end - 1); \
} \
Expand All @@ -87,6 +81,7 @@ static VALUE rb_eGherkinLexingError;
action begin_content {
MARK(content_start, p);
lexer->current_line = lexer->line_number;
lexer->start_col = lexer->content_start - lexer->last_newline - (lexer->keyword_end - lexer->keyword_start) + 2;
}

action begin_pystring_content {
Expand Down Expand Up @@ -249,23 +244,16 @@ static VALUE rb_eGherkinLexingError;
%% write data;

static VALUE
strip_i(VALUE str, VALUE ary)
unindent(VALUE con, int start_col)
{
rb_funcall(str, rb_intern("strip!"), 0);
rb_ary_push(ary, str);

// Gherkin will crash gracefully if the string representation of start_col pushes the pattern past 32 characters
char pat[32];
snprintf(pat, 32, "^[\t ]{0,%d}", start_col);
VALUE re = rb_reg_regcomp(rb_str_new2(pat));
rb_funcall(con, rb_intern("gsub!"), 2, re, rb_str_new2(""));

return Qnil;
}

static VALUE
multiline_strip(VALUE text)
{
VALUE map = rb_ary_new();
VALUE split = rb_str_split(text, "\n");

rb_iterate(rb_each, split, strip_i, map);

return split;
}

static void
Expand All @@ -285,18 +273,19 @@ static void
store_multiline_kw_con(VALUE listener, const char * event_name,
const char * keyword_at, size_t keyword_length,
const char * at, size_t length,
int current_line, int eol)
int current_line, int start_col)
{
VALUE con = Qnil, kw = Qnil, name = Qnil, desc = Qnil;

kw = ENCODED_STR_NEW(keyword_at, keyword_length);
con = ENCODED_STR_NEW(at, length);

VALUE split = multiline_strip(con);
unindent(con, start_col);

VALUE split = rb_str_split(con, "\n");

name = rb_funcall(split, rb_intern("shift"), 0);
desc = rb_ary_join(split, rb_str_new2( \
eol == CRLF_FLAG ? CRLF : LF ));
desc = rb_ary_join(split, rb_str_new2( "\n" ));

if( name == Qnil )
{
Expand All @@ -307,7 +296,7 @@ store_multiline_kw_con(VALUE listener, const char * event_name,
desc = rb_str_new2("");
}
rb_funcall(name, rb_intern("strip!"), 0);
rb_funcall(desc, rb_intern("strip!"), 0);
rb_funcall(desc, rb_intern("rstrip!"), 0);
rb_funcall(listener, rb_intern(event_name), 4, kw, name, desc, INT2FIX(current_line));
}

Expand All @@ -327,13 +316,11 @@ store_pystring_content(VALUE listener,
int current_line)
{
VALUE con = ENCODED_STR_NEW(at, length);
// Gherkin will crash gracefully if the string representation of start_col pushes the pattern past 32 characters
char pat[32];
snprintf(pat, 32, "^[\t ]{0,%d}", start_col);
VALUE re = rb_reg_regcomp(rb_str_new2(pat));

unindent(con, start_col);

VALUE re2 = rb_reg_regcomp(rb_str_new2("\r\\Z"));
VALUE unescape_escaped_quotes = rb_reg_regcomp(rb_str_new2("\\\\\"\\\\\"\\\\\""));
rb_funcall(con, rb_intern("gsub!"), 2, re, rb_str_new2(""));
rb_funcall(con, rb_intern("sub!"), 2, re2, rb_str_new2(""));
rb_funcall(con, rb_intern("gsub!"), 2, unescape_escaped_quotes, rb_str_new2("\"\"\""));
rb_funcall(listener, rb_intern("py_string"), 2, con, INT2FIX(current_line));
Expand All @@ -345,20 +332,6 @@ raise_lexer_error(const char * at, int line)
rb_raise(rb_eGherkinLexingError, "Lexing error on line %d: '%s'. See http://wiki.github.com/aslakhellesoy/gherkin/lexingerror for more information.", line, at);
}

static int
count_char(char char_to_count, char *str) {

int count = 0;
int i = 0;
while(str[i] != '\0') {
if(str[i] == char_to_count) {
count++;
}
i++;
}
return count;
}

static void lexer_init(lexer_state *lexer) {
lexer->content_start = 0;
lexer->content_end = 0;
Expand All @@ -371,7 +344,6 @@ static void lexer_init(lexer_state *lexer) {
lexer->last_newline = 0;
lexer->final_newline = 0;
lexer->start_col = 0;
lexer->eol = LF_FLAG;
}

static VALUE CLexer_alloc(VALUE klass)
Expand Down Expand Up @@ -409,10 +381,6 @@ static VALUE CLexer_scan(VALUE self, VALUE input)
char *data = RSTRING_PTR(input_copy);
size_t len = RSTRING_LEN(input_copy);

if (count_char('\r', data) > (count_char('\n', data) / 2)) {
lexer->eol = CRLF_FLAG;
}

if (len == 0) {
rb_raise(rb_eGherkinLexingError, "No content to lex.");
} else {
Expand Down

0 comments on commit da50e80

Please sign in to comment.