All pastes #902799 Raw Edit

Smeghammer regex problems

public text v1 · immutable
·published 2008-02-13 19:41 UTC
rendered paste body
 ----------------- Form1.vb ------------

''' <summary>
''' Main form for the application. i should really call this something else...
''' </summary>
''' <remarks>
''' we need a format for the lines in config files. i have an idea that might (MIGHT!) just work.
''' the first line of the file would be
'''  <example>;smeghammer-filedef</example> 
'''the second line would be
''' <example>name=python;ext=py|pyc</example>
''' we can then for each line state how the keyword/seperator is:
''' <example>kw // toEOL blue italic</example>
''' <example>kw ... regEx lightblue bold</example>
''' <example>kw insert word green</example>
''' we can parse this using a simple split() operation.
'''</remarks>
Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        shtb.Seperators.Add(" ")
        shtb.Seperators.Add(vbCr) ' seperator
        shtb.Seperators.Add(vbNewLine) ' seperator
        shtb.Seperators.Add(",") ' seperator
        shtb.Seperators.Add(".") ' seperator
        shtb.Seperators.Add(")") ' seperator
        shtb.Seperators.Add("(") ' seperator
        shtb.Seperators.Add("]") ' seperator
        shtb.Seperators.Add("[") ' seperator
        shtb.Seperators.Add("}") ' seperator
        shtb.Seperators.Add("{") ' seperator
        shtb.Seperators.Add("+") ' seperator
        shtb.Seperators.Add("=") ' seperator
        shtb.Seperators.Add(vbTab) ' seperator
        ' fails here VVVVVV (commented out for usability)
        'hilight numbers
        ' shtb.AddHighlightDescriptor(UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.RegEx, "\b(?:[0-9]*\.)?[0-9]+\b", UrielGuy.SyntaxHighlightingTextBox.DescriptorType.Word, Color.Pink, Nothing, False)

    End Sub


    ''' <summary>
    ''' this parses a keyword string.
    ''' </summary>
    ''' <param name="kw">the string to parse</param>
    ''' <remarks>
    '''Type Keywords
    '''Word - only hilight a single word
    '''ToEOL - continue to end of line
    '''ToCloseToken - continue until the closing token is seen
    '''ToEOW - to end of word
    '''
    '''Style keywords
    '''WholeWord - Only if the whole token is equal to the word
    '''StartsWith - If the word starts with the token
    '''Contains - If the word contains the Token
    '''RegEx   the start token is a RegEx. Stoptoken is not used 
    ''' EXAMPLES:
    ''' C++ style comment
    ''' kw::toEOL::startswith:://::none::darkgreen::false
    ''' Vb style comment
    ''' kw:toeol::startswith::'::none::darkgreen::false
    ''' quote
    ''' kw::toclosetoken::startswith::"::"::pink::false
    ''' special namespaces (ie my.settings)
    ''' kw::toEOW::wholeword::my.settings::none::blue::true
    ''' by this basic syntax is
    ''' kw::(kind)::(style)::(opening)::(closing)::(color)::(autocomplete)
    ''' lines with ';' at the start are ignored.
    '''</remarks>
    Private Sub parseKw(ByVal kw As String)
        ' parse out the keyword.
        If (kw(0) = ";") Then
            Exit Sub
            ' comment
        End If
        ' list of arguments
        Dim a As String() = Split(kw, "::")
        ' check if its a seperator
        If (a(0) = "sep") Then
            ' its a seperator. add the second argument and end.
            ' take for example '::' -- in smeghammer, its a seperator for keywords! but if its used here, its toast.
            shtb.Seperators.Add(kw.Substring(3))
            Exit Sub
        End If
        ' is it long enough?
        If (a.Length < 7) Then
            Throw New Exception("BadArgumentCountexception")
        End If
        ' is it the right kind?
        If (a(0) <> "kw") Then
            Dim err As String = "Error!" + vbNewLine
            err += "Keyword identifier not correct!" + vbNewLine
            err += kw + vbNewLine
            err += "Must start with kw"
            MessageBox.Show(err, "parse error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Exit Sub
        End If
        ' we pass the sniff test.
        ' we now add the keyword.
        Dim startKW, endKW, startToken, endToken As String
        ' kw start kind
        startKW = a(1)
        ' kw end kind
        endKW = a(2)
        ' starting token
        startToken = a(3)
        ' ending token
        endToken = a(4)
        ' color
        Dim col As Color = Color.FromName(a(5))
        ' if its an autocomplete
        Dim ShouldUseAsAutocomplete As Boolean
        ' should it be autocomplete?
        If (a(6).ToLower() = "true") Then
            'yes
            ShouldUseAsAutocomplete = True
        Else
            'no
            ShouldUseAsAutocomplete = False
        End If

        Dim token_type, descriptor_type As Object
        If (startKW.ToLower() = "word") Then
            token_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorType.Word
        ElseIf (startKW.ToLower() = "toeol") Then
            token_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorType.ToEOL
        ElseIf (startKW.ToLower() = "toeow") Then
            token_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorType.ToEOW
        ElseIf (startKW.ToLower() = "toclosetoken") Then
            token_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorType.ToCloseToken
        Else
            Throw New Exception("Bad argument on token type. undefined token kind " + startKW)
        End If
        If (endKW.ToLower() = "wholeword") Then
            descriptor_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.WholeWord
            '        ElseIf (endKW.ToLower() = "regex") Then
            '            descriptor_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.RegEx
        ElseIf (endKW.ToLower() = "contains") Then
            descriptor_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.Contains
        ElseIf (endKW.ToLower() = "startswith") Then
            descriptor_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.StartsWith
        Else
            Throw New Exception("Bad Argument on Token Descriptor. unknow descriptor " + endKW)
        End If
        '        If (descriptor_type = UrielGuy.SyntaxHighlightingTextBox.DescriptorRecognition.RegEx) Then
        'Me.shtb.AddHighlightDescriptor(descriptor_type, startToken, token_type, col, Nothing, ShouldUseAsAutocomplete)
        'Else
        Me.shtb.AddHighlightDescriptor(descriptor_type, startToken, token_type, endToken, col, Nothing, ShouldUseAsAutocomplete)
        'End If

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            Me.parseKw(TextBox1.Text)
        Catch ex As Exception
            Label1.Text = ex.Message

        End Try
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim cp As New ColorDialog
        If (cp.ShowDialog <> Windows.Forms.DialogResult.Cancel) Then
            Button2.BackColor = cp.Color
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        shtb.HighlightDescriptors.Clear()
        Dim a As New OpenFileDialog
        a.Title = "Select a dictionary"
        a.Filter = "Smeghammer definitions|*.shd|All files|*.*"
        If (a.ShowDialog() <> Windows.Forms.DialogResult.Cancel) Then
            ' initialize a fileIOReader.
            ' we want something like this:
            ' this is how it should look like:
            ' kw::(kind)::(style)::(open)::(close)::(color)::(autocomplete)
            ' for now we are NOT supporting regex's
            Dim buffer As String() = IO.File.ReadAllLines(a.FileName)
            ' get each line in the string
            Dim line As String
            Dim count As Integer
            count = 1
            For Each line In buffer
                ' get the parser running
                Try
                    If (line <> "") Then
                        parseKw(line)
                    End If

                Catch ex As Exception
                    MessageBox.Show("Line " + count.ToString() + vbNewLine + "Parse Error:" + vbNewLine + ex.Message + vbNewLine + "Ending Parsing" + vbNewLine + "Line is:" + vbNewLine + line, "parse error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    If (MessageBox.Show("Clear highlight descriptors?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
                        shtb.HighlightDescriptors.Clear()
                    End If
                    Exit Sub
                End Try
                count += 1
            Next
        End If
    End Sub
End Class


--- end form1.vb ---

-- smeg def file --
kw::word::wholeword::kw::none::blue::false
kw::word::wholeword::sep::none::blue::false

; type
kw::word::wholeword::word::none::red::true
kw::word::wholeword::toclosetoken::none::red::true
kw::word::wholeword::toeol::none::red::true
kw::word::wholeword::toeow::none::red::true

;style
kw::word::wholeword::wholeword::none::red::true
kw::word::wholeword::startswith::none::red::true
-- end smegdef.txt --