Skip to content

Commit

Permalink
feat: allows braces on citekey
Browse files Browse the repository at this point in the history
  • Loading branch information
mapreal19 committed Jul 19, 2021
1 parent 28fb1ba commit 5a3c5f1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
11 changes: 7 additions & 4 deletions lib/bibtex/lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class Lexer
string: /string/io,
comment: /comment\b/io,
preamble: /preamble\b/io,
key: %r{\s*[[:alpha:][:digit:] /:_!$\?\.%+;&\*'"-]+,}io,
optional_key: %r{\s*[[:alpha:][:digit:] /:_!$\?\.%+;&\*'"-]*,}io
key: %r{\s*[[:alpha:][:digit:] /:_!$\?\.%+;&\*'"-\{\}]+,}io,
optional_key: %r{\s*[[:alpha:][:digit:] /:_!$\?\.%+;&\*'"-\{\}]*,}io
}

MODE = Hash.new(:meta).merge(
Expand Down Expand Up @@ -120,7 +120,7 @@ def next_token
@stack.shift
end

# Returns true if the lexer is currenty parsing a BibTeX object.
# Returns true if the lexer is currently parsing a BibTeX object.
def bibtex_mode?
MODE[@mode] == :bibtex
end
Expand Down Expand Up @@ -308,7 +308,10 @@ def enter_object
push([:LBRACE, '{'])
@mode = :content if @brace_level > 1 || @brace_level == 1 && active?(:comment)

push [:KEY, @scanner.matched.chop.strip] if @scanner.scan(Lexer.patterns[allow_missing_keys? ? :optional_key : :key])
if @scanner.scan(Lexer.patterns[allow_missing_keys? ? :optional_key : :key])
key = @scanner.matched.chop.strip.tr('{}', '')
push [:KEY, key]
end
end

else
Expand Down
18 changes: 11 additions & 7 deletions test/bibtex/test_lexer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,37 @@ class LexerTest < Minitest::Spec
end

it 'strips line breaks by default' do
Lexer.new.analyse(%(@string{ x = "foo\nbar" })).stack[-3].must_be :==,
_(Lexer.new.analyse(%(@string{ x = "foo\nbar" })).stack[-3]).must_be :==,
[:STRING_LITERAL, 'foo bar']
end

it 'strips whitespace after line breaks by default' do
Lexer.new.analyse(%(@string{ x = "foo\n bar" })).stack[-3].must_be :==,
_(Lexer.new.analyse(%(@string{ x = "foo\n bar" })).stack[-3]).must_be :==,
[:STRING_LITERAL, 'foo bar']
end

it 'matches KEY tokens' do
Lexer.new.analyse('@misc{foo, }').symbols.must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
_(Lexer.new.analyse('@misc{foo, }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
end

it 'matches KEY tokens with non-ascii characters' do
Lexer.new.analyse('@misc{löwe, }').symbols.must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
_(Lexer.new.analyse('@misc{löwe, }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
end

it 'matches KEY tokens after whitespace' do
Lexer.new.analyse('@misc{ foo, }').symbols.must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
_(Lexer.new.analyse('@misc{ foo, }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
end

it 'matches KEY tokens with braces' do
_(Lexer.new.analyse('@misc{foo:{123}, }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
end

it "doesn't start a comment for types starting with but not equal @comment" do
Lexer.new.analyse('@commentary{staudinger, }').symbols.must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
_(Lexer.new.analyse('@commentary{staudinger, }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :KEY, :RBRACE, false]
end

it "doesn't start a preamble for types starting with but not equal @preamble" do
Lexer.new.analyse('@preamblestring{ preamble }').symbols.must_be :==, [:AT, :NAME, :LBRACE, :NAME, :RBRACE, false]
_(Lexer.new.analyse('@preamblestring{ preamble }').symbols).must_be :==, [:AT, :NAME, :LBRACE, :NAME, :RBRACE, false]
end
end
end

0 comments on commit 5a3c5f1

Please sign in to comment.