Parser catch-all参数列表

时间:2015-03-05 19:12:54

标签: c# sql antlr4

好的,我在这上面已经失去了两天,并且在Stack上查看了与ANTLR4有关的所有其他答案,但我无法解决这个问题。

我正在解析一个如下所示的SQL表定义:

CREATE TABLE [dbo].[GeographicZones](
    [GeographicZoneID] [int] IDENTITY(1,1) NOT NULL,
    [Township] [smallint] NOT NULL,
    [Range] [smallint] NOT NULL,
    [Section] [tinyint] NOT NULL,
    [TownshipRange]  AS ((((('T'+CONVERT([varchar](3),[Township],0))+case when [Township]>(0) then 'N' else 'S' end)+'R')+CONVERT([varchar](3),[Range],0))+case when [Range]>(0) then 'E' else 'W' end),
    [LAOZone] [bit] NOT NULL,
    [MSDZone] [bit] NOT NULL,
    [ParrottZone] [bit] NOT NULL,
 CONSTRAINT [PK_GeographicZones] PRIMARY KEY CLUSTERED 
(
    [GeographicZoneID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
 CONSTRAINT [UK_GeographicZones_TRS] UNIQUE NONCLUSTERED 
(
    [Township] ASC,
    [Range] ASC,
    [Section] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

我无法解决的关键位是TownshipRange计算列。我不关心()语句之后的AS之间的内容,并且想要忽略逗号分隔符。

以下是computedColumn规则:

/**
TSql only. In code generation, this is mapped as a read-only
property. In translation, special comments are placed that
indicate that this should be a computed column.
*/
computedColumn
    : columnName AS anyExpr ( PERSISTED ( NOT NULL )? )?
    {
    logInfo( "Computed column {0}",
        $columnName.ctx.GetText() );
    }
    ;

以下是anyExpr规则,曾用于ANTLR4 4.0.1:

/**
Use for a parameter list (blah blah) where we don't care
about the contents. Uses a sempred to go until $open == 0.
Note that the right paren is missing from the end of the rule -
this is by design.
*/
anyExpr
locals [int open = 1]
    : Lp
    ( {$open > 0}? 
        ( ~( Lp | Rp )
        | Lp { $open++; }
        | Rp { $open--; }
        )
    )*
    ;

以下是使用ANTLR4 4.3.0记录的输出:

INFO    Computed column [TownshipRange]
line 579:22 extraneous input '(' expecting {Comma, Rp}

它失败的(是第一个嵌套的左边的paren。但无论开幕式之后的第一个角色是什么,它都会立即失败并发出相同的警告。

我的“全能”逻辑有什么问题,以及在4.0.1和4.3之间发生什么变化会破坏这个?

作为旁注(可能与问题有关)4.3使用AGES以LL模式解析整个SQL文件,因此我使用SLL。 4.0.1识别LL和SLL中的规则,并且分析时间差异很小。

4.0.1可能只是忽略了无关的输入而我的anyExpr一直都被打破了吗?

1 个答案:

答案 0 :(得分:2)

为什么不自己引用anyExpr?

anyExpr
 : Lp ( ~( Lp | Rp ) | anyExpr )* Rp 
 ;

这可能会导致更快的解析(我自己也没试过!):

anyExpr
 : Lp ( ~( Lp | Rp )+ | anyExpr )* Rp 
 ;