VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
END
Attribute VB_Name = "ParserNPA"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
' ====== Legal document title parser =======
' Shared module version: 20220614
' Tested in:
' Depends on:
' Required reference:  z_ParserRegex, PC_InfoNPA
Option Explicit

Private rxTitleFirst_ As RegExp
Private rxTitleLast_ As RegExp
Private rxCreatorPrefix_ As RegExp
Private rxTitle_ As RegExp
Private rxImmediate_ As RegExp

Public data_ As New PC_InfoNPA

Private Sub Class_Initialize()
  Set rxTitleFirst_ = New RegExp
  rxTitleFirst_.Pattern = P_NPA_TITLE_FIRST
  rxTitleFirst_.Global = False
  
  Set rxTitleLast_ = New RegExp
  rxTitleLast_.Pattern = P_NPA_PROPS_FIRST
  rxTitleLast_.Global = False
  
  Set rxCreatorPrefix_ = New RegExp
  rxCreatorPrefix_.Pattern = P_NPA_DOCTYPE
  rxCreatorPrefix_.Global = False
  
  Set rxTitle_ = New RegExp
  rxTitle_.Pattern = P_NPA_COMPOSITE_TITLE
  rxTitle_.Global = False
  
  Set rxImmediate_ = New RegExp
  rxImmediate_.Pattern = P_NPA_IMMEDIATE
  rxImmediate_.Global = False
End Sub

Public Function Test(target$) As Boolean
  Test = rxTitleLast_.Test(target)
  If Not Test Then Test = rxTitleFirst_.Test(target)
End Function

Public Function Parse(target$) As Boolean
  Set data_ = New PC_InfoNPA
  Dim matches As Object
  Dim isTitleLast As Boolean
  isTitleLast = rxTitleLast_.Test(target)
  If Not isTitleLast Then
    If Not rxTitleFirst_.Test(target) Then
      Parse = False
      Exit Function
    End If
  End If
 
  Parse = True
  If isTitleLast Then
    Set matches = rxTitleLast_.Execute(target)
    data_.prefix_ = matches.Item(0).SubMatches(0)
    data_.date_ = matches.Item(0).SubMatches(1)
    data_.number_ = Trim(matches.Item(0).SubMatches(2))
    data_.title_ = Trim(matches.Item(0).SubMatches(3))
  Else
    Set matches = rxTitleFirst_.Execute(target)
    data_.title_ = Trim(matches.Item(0).SubMatches(0))
    data_.prefix_ = Trim(matches.Item(0).SubMatches(1))
    data_.date_ = Trim(matches.Item(0).SubMatches(2))
    data_.number_ = Trim(matches.Item(0).SubMatches(3))
  End If
  
  Call ParsePrefix
  Call ParseTitle
End Function

Public Function GetData() As Collection
  Set GetData = data_.AsCollection()
End Function

Public Function GetDataDescription() As Scripting.Dictionary
  Set GetDataDescription = data_.AsDescription()
End Function

Public Function Transform(sText$, sParam$) As String
  Transform = sText
End Function

' =======
Private Function ParsePrefix()
  If data_.prefix_ = vbNullString Then _
    Exit Function
  If Not rxCreatorPrefix_.Test(data_.prefix_) Then
    data_.creator_ = data_.prefix_
    Exit Function
  End If
  
  Dim matches As Object: Set matches = rxCreatorPrefix_.Execute(data_.prefix_)
  data_.docType_ = CapitalizeFirstLetter(Trim(matches.Item(0).Value))
  If Len(data_.docType_) <> Len(data_.prefix_) Then _
    data_.creator_ = Trim(Right(data_.prefix_, Len(data_.prefix_) - Len(data_.docType_)))
  Call FixTypeCase
End Function

Private Function ParseTitle()
  Dim theTitle$: theTitle = data_.title_
  If theTitle = vbNullString Then _
    Exit Function
    
  data_.isTimeBound_ = rxImmediate_.Test(theTitle)
    
  If Not rxTitle_.Test(theTitle) Then _
    Exit Function
  
  Dim matches As Object: Set matches = rxTitle_.Execute(theTitle)
  data_.titlePrefix_ = Trim(matches.Item(0).SubMatches(0))
  data_.titleDoc_ = Trim(matches.Item(0).SubMatches(1))
End Function

Private Function FixTypeCase()
  If Len(data_.docType_) = 0 Then _
    Exit Function
    
  Select Case Left(data_.docType_, 1)
    Case "Д"
      data_.docType_ = Replace(data_.docType_, "Директивы", "Директива")
      data_.docType_ = Replace(data_.docType_, "Директивой", "Директива")
      data_.docType_ = Replace(data_.docType_, "Директиве", "Директива")
    Case "З"
      data_.docType_ = Replace(data_.docType_, "Заключением", "Заключение")
      data_.docType_ = Replace(data_.docType_, "Заключения", "Заключение")
      data_.docType_ = Replace(data_.docType_, "Заключению", "Заключение")
      data_.docType_ = Replace(data_.docType_, "Законом", "Закон")
      data_.docType_ = Replace(data_.docType_, "Закона", "Закон")
      data_.docType_ = Replace(data_.docType_, "Закону", "Закон")
    Case "И"
      data_.docType_ = Replace(data_.docType_, "Информационного письма", "Информационное письмо")
      data_.docType_ = Replace(data_.docType_, "Информационным письмом", "Информационное письмо")
      data_.docType_ = Replace(data_.docType_, "Информационному письму", "Информационное письмо")
    Case "П"
      data_.docType_ = Replace(data_.docType_, "Приказом", "Приказ")
      data_.docType_ = Replace(data_.docType_, "Приказа", "Приказ")
      data_.docType_ = Replace(data_.docType_, "Приказу", "Приказ")
      data_.docType_ = Replace(data_.docType_, "Письмом", "Письмо")
      data_.docType_ = Replace(data_.docType_, "Письма", "Письмо")
      data_.docType_ = Replace(data_.docType_, "Письму", "Письмо")
    Case "Р"
      data_.docType_ = Replace(data_.docType_, "Распоряжением", "Распоряжение")
      data_.docType_ = Replace(data_.docType_, "Распоряжения", "Распоряжение")
      data_.docType_ = Replace(data_.docType_, "Распоряжению", "Распоряжение")
      data_.docType_ = Replace(data_.docType_, "Решением", "Решение")
      data_.docType_ = Replace(data_.docType_, "Решения", "Решение")
      data_.docType_ = Replace(data_.docType_, "Решению", "Решение")
    Case "У"
      If data_.docType_ = "Указа" Then
        data_.docType_ = "Указ"
      Else
        data_.docType_ = Replace(data_.docType_, "Указом", "Указ")
        data_.docType_ = Replace(data_.docType_, "Указу", "Указ")
        data_.docType_ = Replace(data_.docType_, "Указания", "Указание")
        data_.docType_ = Replace(data_.docType_, "Указанием", "Указание")
        data_.docType_ = Replace(data_.docType_, "Указанию", "Указание")
      End If
    Case "Ф"
      data_.docType_ = Replace(data_.docType_, "Федеральным", "Федеральный")
      data_.docType_ = Replace(data_.docType_, "Федерального", "Федеральный")
      data_.docType_ = Replace(data_.docType_, "Федеральному", "Федеральный")
      data_.docType_ = Replace(data_.docType_, "законом", "закон")
      data_.docType_ = Replace(data_.docType_, "закона", "закон")
      data_.docType_ = Replace(data_.docType_, "закону", "закон")
  End Select
End Function

