From 33afd44e0ee03ca441ee0b4425aa8841844c7538 Mon Sep 17 00:00:00 2001 From: Krom Date: Tue, 2 Apr 2019 13:34:16 +0300 Subject: [PATCH] Add comments parsing (not reflected in API) --- src/main/antlr/M3uLexer.g4 | 6 ++++++ src/main/antlr/M3uParser.g4 | 3 ++- .../kotlin/ru/grushetsky/m3uparser/DefaultM3uParser.kt | 2 +- src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt | 2 +- src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt | 8 ++++++++ src/test/resources/entry_with_comments.txt | 3 +++ 6 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 src/test/resources/entry_with_comments.txt diff --git a/src/main/antlr/M3uLexer.g4 b/src/main/antlr/M3uLexer.g4 index b37e974..9ffe189 100644 --- a/src/main/antlr/M3uLexer.g4 +++ b/src/main/antlr/M3uLexer.g4 @@ -17,6 +17,7 @@ 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; @@ -24,6 +25,10 @@ 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: '"'; @@ -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]; \ No newline at end of file diff --git a/src/main/antlr/M3uParser.g4 b/src/main/antlr/M3uParser.g4 index 52d060c..d443974 100644 --- a/src/main/antlr/M3uParser.g4 +++ b/src/main/antlr/M3uParser.g4 @@ -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; diff --git a/src/main/kotlin/ru/grushetsky/m3uparser/DefaultM3uParser.kt b/src/main/kotlin/ru/grushetsky/m3uparser/DefaultM3uParser.kt index 4fdcaf7..9864797 100644 --- a/src/main/kotlin/ru/grushetsky/m3uparser/DefaultM3uParser.kt +++ b/src/main/kotlin/ru/grushetsky/m3uparser/DefaultM3uParser.kt @@ -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()) ) } diff --git a/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt b/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt index 6b90d24..6338756 100644 --- a/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt +++ b/src/main/kotlin/ru/grushetsky/m3uparser/PlaylistEntry.kt @@ -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 ) \ No newline at end of file diff --git a/src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt b/src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt index b6a5660..28bed4a 100644 --- a/src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt +++ b/src/test/kotlin/ru/grushetsky/m3uparser/M3uParserTest.kt @@ -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", diff --git a/src/test/resources/entry_with_comments.txt b/src/test/resources/entry_with_comments.txt new file mode 100644 index 0000000..5de67e7 --- /dev/null +++ b/src/test/resources/entry_with_comments.txt @@ -0,0 +1,3 @@ +#EXTINF:-1,Channel 1 +#This comment should be ignored +hls:http://hls-prefixed-url/