module FoodFishParser::Strict::Grammar

grammar FaoArea
  include Common

  rule fao_area_list_enclosures
    ( '(' ws* fao_area_list ws* ')' ) /
    ( '|' ws* fao_area_list ) /
    fao_area_list
  end

  rule fao_area_list
    fao_area_indicator ws*
    ':'? ws*
    fao_area_code
    (
      ( '/' fao_area_code )+ /
      ( ( ',' ws* fao_area_code )+ ws* comma? ws* and ws+ fao_area_code ) /
      ( ',' ws* fao_area_code )+
    )?
  end

  rule fao_area_indicator
    ( 'vangstgebied'i ws+ )?
    ( 'FAO'i / 'FA0'i )
    ( ( dash / ws+ ) 'gebied'i / 'area'i )? ( ws* 'nr'i '.'? )?
  end

  rule fao_area_code
    fao_area_major_code
    (
      ( ws* '(' ws* fao_area_sub_range ws* ')' ) /
      ( fao_area_sub_range )
    )?
    <FaoAreaCodeNode>
  end

  rule fao_area_major_code
    ( '0' [0-9] [0-9] ) / ( [0-9] [0-9] )
  end

  rule fao_area_sub_range
    fao_area_sub_code
    ( ws* dash ws* fao_area_sub_code )?
  end

  rule fao_area_sub_code
    (
      # IVX for roman numals, but l for incorrectly OCR-ed text
      ( ( dash / '/' / ws* )? [ivxIVXl]+ ) /
      ( dash [0-9]+ )
    )
    fao_area_suffix?
    (
      ws* '(' ws* [[:digit:]]+ ( ws* dash ws* [[:digit:]]+ )? ws* ')'
    )?
  end

  rule fao_area_suffix
    [abcdABCD]
  end
end

end