ANTLR 规则匹配不带引号或带引号的多行字符串

ANTLR rules to match unquoted or quoted multiline string

我希望我的语法能够匹配由换行符(\r\n 或 \n)终止的单行字符串赋值,可能在末尾带有注释,或者匹配多行赋值,表示为用双引号。例如:

key = value
key = spaces are allowed
key = until a new line or a comment # this is a comment
key = "you can use quotes as well" # this is a comment
key = "and
with quotes 
you can also do 
multiline"

这可行吗?我一直在抨击这个问题,除了多线之外,一切正常。看起来很简单,但是规则就是不匹配。

补充:这只是更大语法的一部分。

查看您的示例输入:

# This is the most simple configuration
title = "FML rulez"

# We use ISO notations only, so no local styles
releaseDateTime = 2020-09-12T06:34

# Multiline strings
description = "So,
I'm curious 
where this will end."

# Shorcut string; no quotes are needed in a simple property style assignment
# Or if a string is just one word. These strings are trimmed.
protocol = http

# Conditions allow for overriding, best match wins (most conditions)
# If multiple condition sets equally match, the first one will win.
title[env=production] = "One config file to rule them all"
title[env=production & os=osx] = "Even on Mac"

# Lists
hosts = [alpha, beta]

# Hierarchy is implemented using groups denoted by curly brackets
database {

    # indenting is allowed and encouraged, but has no semantic meaning
    url = jdbc://...
    user = "admin"

    # Strings support default encryption with a external key file, like maven
    password = "FGFGGHDRG#$BRTHT%G%GFGHFH%twercgfg"

    # groups can nest
    dialect {
        database = postgres
    }
}

servers {
    # This is a table:
    # - the first row is a header, containing the id's
    # - the remaining rows are values
    | name     | datacenter | maxSessions | settings                    |
    | alpha    | A          | 12          |                             |
    | beta     | XYZ        | 24          |                             |
    | "sys 2"  | B          | 6           |                             |
    # you can have sub blocks, which are id-less groups (id is the column)
    | gamma    | C          | 12          | {breaker:true, timeout: 15} |
    # or you reference to another block
    | tango    | D          | 24          | $environment                |
}

# environments can be easily done using conditions
environment[env=development] {
    datasource = tst
}
environment[env=production] {
    datesource = prd
}

我会选择这样的东西:

grammar TECL;

input_file
 : configs EOF
 ;

configs
 : NL* ( config ( NL+ config )* NL* )?
 ;

config
 : property
 | group
 | table
 ;

property
 : WORD conditions? ASSIGN value
 ;

group
 : WORD conditions? NL* OBRACE configs CBRACE
 ;

conditions
 : OBRACK property ( AMP property )* CBRACK
 ;

table
 : row ( NL+ row )*
 ;

row
 : PIPE ( col_value PIPE )+
 ;

col_value
 : ~( PIPE | NL )*
 ;

value
 : WORD
 | VARIABLE
 | string
 | list
 ;

string
 : STRING
 | WORD+
 ;

list
 : OBRACK ( value ( COMMA value )* )? CBRACK
 ;

ASSIGN : '=';
OBRACK : '[';
CBRACK : ']';
OBRACE : '{';
CBRACE : '}';
COMMA  : ',';
PIPE   : '|';
AMP    : '&';

VARIABLE
 : '$' WORD
 ;

NL
 : [\r\n]+
 ;

STRING
 : '"' ( ~[\"] | '\' . )* '"'
 ;

WORD
 : ~[ \t\r\n[\]{}=,|&]+
 ;

COMMENT
 : '#' ~[\r\n]* -> skip
 ;

SPACES
 : [ \t]+ -> skip
 ;

它将解析以下解析树中的示例:

输入:

key = value
key = spaces are allowed
key = until a new line or a comment # this is a comment
key = "you can use quotes as well" # this is a comment
key = "and
with quotes 
you can also do 
multiline"

进入以下内容:

For now: multiline quoted works, spaces in unquoted string not.

正如您在上面的树中看到的那样,它确实有效。我怀疑您使用了现有语法中的部分语法,但那不起作用。

[...] and am I the process inserting the actions.

我不会在您的语法中嵌入操作(目标代码):这会使阅读变得困难,而且对语法进行更改也更难。当然,您的语法只适用于一种语言。最好使用 listener or visitor 而不是这些操作。

祝你好运!