Skip to content

Commit

Permalink
Add comments parsing (not reflected in API)
Browse files Browse the repository at this point in the history
  • Loading branch information
kirill-gr committed Apr 3, 2019
1 parent 8428946 commit 33afd44
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main/antlr/M3uLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ CHANNEL_DESC_NEWLINE: NEWLINES -> skip;
CHANNEL_DESC_TEXT: CHANNEL_DESC_SAFE_CHAR+;
CHANNEL_DESC_GROUP: '#EXTGRP' -> pushMode(CHANNEL_GROUP);
CHANNEL_DESC_CHANNEL_START: EXTINF -> popMode;
CHANNEL_GROUP_HASH: '#' -> pushMode(COMMENT);

mode CHANNEL_GROUP;

CHANNEL_GROUP_COLON: ':';
CHANNEL_GROUP_TEXT: CHANNEL_GROUP_SAFE_CHAR+;
CHANNEL_GROUP_NEWLINE: NEWLINES -> popMode, skip;

mode COMMENT;
COMMENT_TEXT: COMMENT_SAFE_CHAR+;
COMMENT_NEWLINE: NEWLINES -> popMode, skip;

fragment NEWLINES: ('\r\n' | '\r' | '\n');
fragment EXTINF: '#EXTINF';
fragment QUOTE: '"';
Expand All @@ -32,3 +37,4 @@ fragment DIGIT: [0-9];
fragment SAFE_CHAR: ~[":= \t,\r\n];
fragment CHANNEL_DESC_SAFE_CHAR: ~[#\r\n];
fragment CHANNEL_GROUP_SAFE_CHAR: ~[:\r\n];
fragment COMMENT_SAFE_CHAR: ~[\r\n];
3 changes: 2 additions & 1 deletion src/main/antlr/M3uParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ file_header: FILE_START parameters;
entries: entry_info+;
entry_info: (CHANNEL_START | CHANNEL_DESC_CHANNEL_START) COLON length parameters COMMA enrty_basic_info;
length: NUMBER;
enrty_basic_info: entry_name (CHANNEL_DESC_GROUP CHANNEL_GROUP_COLON group_name)? entry_uri;
enrty_basic_info: entry_name (CHANNEL_DESC_GROUP CHANNEL_GROUP_COLON group_name)? (CHANNEL_GROUP_HASH comment)* entry_uri;
entry_name: CHANNEL_DESC_TEXT;
group_name: CHANNEL_GROUP_TEXT;
comment: COMMENT_TEXT;
entry_uri: CHANNEL_DESC_TEXT;
parameters: parameter*;
parameter: key EQUALS value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class DefaultM3uParser(input: String) : M3uParserWrapper {
parsedEntry.enrty_basic_info().entry_name().text.trim(),
parsedEntry.enrty_basic_info().entry_uri().text.trim(),
parsedEntry.length().text.toInt(),
parsedEntry.enrty_basic_info().group_name().text.trim(),
parsedEntry.enrty_basic_info().group_name()?.text?.trim(),
createParameterMap(parsedEntry.parameters().parameter())
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ data class PlaylistEntry(
val name: String,
val path: String,
val length: Int,
val group: String,
val group: String?,
val parameters: Map<String, String>
)
8 changes: 8 additions & 0 deletions src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ class M3uParserTest {
assertThat(entryParser.enrty_basic_info().entry_uri().text).isEqualTo("http://1.2.3.4:1234/udp/4.3.2.1:5678")
}

@Test
fun `comments in entry are ignored`() {
val entryParser = getParser(getResourceAsString("/entry_with_comments.txt")).entry_info()

assertThat(entryParser.enrty_basic_info().entry_name().text).isEqualTo("Channel 1")
assertThat(entryParser.enrty_basic_info().entry_uri().text).isEqualTo("hls:http://hls-prefixed-url/")
}

@ParameterizedTest
@CsvSource(
"header_example.txt, 5",
Expand Down
3 changes: 3 additions & 0 deletions src/test/resources/entry_with_comments.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#EXTINF:-1,Channel 1
#This comment should be ignored
hls:http://hls-prefixed-url/

0 comments on commit 33afd44

Please sign in to comment.