1
votes

I am parsing swift code with the antlr 4.7 parser. In the g4 the Interpolated_string_literal token is defined like this:

Interpolated_string_literal : '"' Interpolated_text_item* '"' ;
fragment
Interpolated_text_item
  : '\\(' (Interpolated_string_literal | Interpolated_text_item)+ ')' // nested strings allowed
  | Quoted_text_item
  ;

My problem is that i need the parser to know about the components of the interpolated text item. I understand that since it is defined as fragment this cannot work. So i assumed if i remove the fragment key word it will be ok. BUT, after removing it i start getting many errors such as:

line 9:6 extraneous input ' ' expecting {'for', 'in', 'var', 'typealias', 'struct', 'class', 'enum', 'protocol', 'func', 'get', 'set', 'willSet', 'didSet', 'mutating', 'nonmutating', 'indirect', 'prefix', 'operator', 'postfix', 'infix', 'precedence', 'associativity', 'left', 'right', 'none', 'convenience', 'dynamic', 'final', 'lazy', 'optional', 'override', 'required', 'unowned', 'weak', 'Protocol', 'Type', Identifier, '.', '<', '>', '!', '?', '&', '-', '=', '|', '/', '+', '', '%', '^', '~', Operator_head_other} line 11:5 no viable alternative at input 'class ' line 9:6 extraneous input ' ' expecting {'for', 'in', 'var', 'typealias', 'struct', 'class', 'enum', 'protocol', 'func', 'get', 'set', 'willSet', 'didSet', 'mutating', 'nonmutating', 'indirect', 'prefix', 'operator', 'postfix', 'infix', 'precedence', 'associativity', 'left', 'right', 'none', 'convenience', 'dynamic', 'final', 'lazy', 'optional', 'override', 'required', 'unowned', 'weak', 'Protocol', 'Type', Identifier, '.', '<', '>', '!', '?', '&', '-', '=', '|', '/', '+', '', '%', '^', '~', Operator_head_other}

I don't understand why changing this from fragment to token caused all these errors. Any leads?

This is the code with the first errors:

import UIKit
import CoreData
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
    var window: UIWindow?
    var locManager: CLLocationManager?

Thanks,

Roy

1

1 Answers

0
votes

I see the error says extraneous input ' ' is there a rule to handle empty spaces? If not can you try adding the following to the grammar to handle empty spaces and check if it makes any difference.

SPACE:    [ \t\r\n]+    -> channel(HIDDEN);