Skip to content

Commit

Permalink
Add CSV Schema lexer (#1039)
Browse files Browse the repository at this point in the history
This commit adds a lexer for CSV Schema.
  • Loading branch information
filipegarcia authored and pyrmont committed Sep 17, 2019
1 parent a09305e commit 46ce2ac
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/rouge/demos/csvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version 1.1
@totalColumns 5
@separator ','
Transaction_Date: xDate
Transaction_ID: notEmpty
Originator_Name: notEmpty
Originator_Address: any("yes","no")
Originator_Country: notEmpty
44 changes: 44 additions & 0 deletions lib/rouge/lexers/csvs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

module Rouge
module Lexers
class CSVS < RegexLexer
tag 'csvs'
title "csvs"
desc 'The CSV Schema Language (digital-preservation.github.io)'
filenames '*.csvs'

state :root do
rule %r/\s+/m, Text

rule %r(//[\S\t ]*), Comment::Single
rule %r(/\*[^*]*\*/)m, Comment::Multiline

rule %r/(version)( )(\d+\.\d+)/ do
groups Keyword, Text::Whitespace, Num::Float
end

rule %r/T?\d{2}:\d{2}:\d{2}(\.\d{5})?(Z|(?:[-+]\d{2}:\d{2}))?/, Literal::Date
rule %r/\d{4}-\d{2}-\d{2}/, Literal::Date
rule %r/\d{2}\/\d{2}\/\d{4}/, Literal::Date

rule %r((\d+[.]?\d*|\d*[.]\d+)(e[+-]?[0-9]+)?)i, Num::Float
rule %r/\d+/, Num::Integer

rule %r/@\w+/, Keyword::Pseudo

rule %r/[-.\w]+:/, Name::Variable
rule %r/^"[^"]+"/, Name::Variable
rule %r/\$([-.\w]+|("[^"]+"))\/?/, Name::Variable

rule %r/[A-Z]+/i, Keyword

rule %r/"[^"]*"/, Str::Double
rule %r/'[^\r\n\f']'/, Str::Char

rule %r/[,()*]/, Punctuation
end
end
end
end
14 changes: 14 additions & 0 deletions spec/lexers/csvs_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# -*- coding: utf-8 -*- #
# frozen_string_literal: true

describe Rouge::Lexers::CSVS do
let(:subject) { Rouge::Lexers::CSVS.new }

describe 'guessing' do
include Support::Guessing

it 'guesses by filename' do
assert_guess :filename => 'foo.csvs'
end
end
end
32 changes: 32 additions & 0 deletions spec/visual/samples/csvs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
version 1.2
@totalColumns 3
name: notEmpty
age: range(0, 120)
gender: is("m") or is("f") or is("t") or is("n")

@separator TAB
@quoted
@totalColumns 21
@permitEmpty
@ignoreColumnNameCase

//This Comment is a Single Line Comment it terminates at this line break
a_column:

/*This Comment is a Multi Line Comment:
it can go on for as many lines as you like, until you type*/
a_column:

a_column: is("some string") and $b_column/starts("some string") //here two tests are combined on a single line, the second test here looks to the second column
b_column: //to check it's value starts with "some string"

a_column: is("some string") //the contents of a_column must be the string "some string"
a_column: is($a_column) //the contents of a_column must be the value held in a_column, treated as a string

a_column: range(*, 10)

a_column: xDateTime(2014-10-04T00:00:01Z,2015-12-03T23:59:59) //the value of a_column must be a valid xDateTime and between the two xDateTimes shown (inclusive)
a_column: xDateTime(2014-10-04T00:00:01+02:00,2015-12-03T23:59:59+02:00) //the value of a_column must be a valid xDateTime and between the two xDateTimes shown (inclusive)
a_column: xDateTime(2014-10-04,2015-12-03) //the value of a_column must be a valid xDate and between the two xDates shown (inclusive)
a_column: xTime(00:00:01+02:00,2015-12-03T23:59:59+02:00) //the value of a_column must be a valid xTime and between the two xTimes shown (inclusive)
a_column: ukDate(04/10/2014,03/12/2015) //the value of a_column must be a valid ukDate and between the two ukDates shown (inclusive)

0 comments on commit 46ce2ac

Please sign in to comment.