#
#
#  Copyright (c) 2016, Red Hat, Inc.
#  Copyright (c) 2016, Masatake YAMATO
#
#  Author: Masatake YAMATO <yamato@redhat.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
#
#
--langdef=Man
--map-Man=+.1
--map-Man=+.2
--map-Man=+.3
--map-Man=+.4
--map-Man=+.5
--map-Man=+.6
--map-Man=+.7
--map-Man=+.8
--map-Man=+.9

# For perl modules
--map-Man=+.3pm

# For systemtap
--map-Man=+.3stap
--map-Man=+.7stap

--kinddef-Man=t,title,titles
--kinddef-Man=s,section,sections
--kinddef-Man=S,subsection,sub sections

--_scopesep-Man=*/*:""

--_tabledef-Man=main
--_tabledef-Man=section
--_tabledef-Man=sectionheading
--_tabledef-Man=subsection
--_tabledef-Man=subsectionheading

--_prelude-Man={{
    % adjustment:int FILL-END-OF-SCOPE -
    %
    % Note: The group 1 of a regex matching is assumed.
    %       An entry on the scope stack is assumed.
    /fill-end-of-scope {
         _scopetop pop exch
         % scope-top:int adjustment:int
         @1 _matchloc2line exch
         % scope-top:int line adjustment:int
         2 copy gt {
             sub end:
         } {
             pop pop pop
         } ifelse
    } def
    % adjustment:int /replace HEADING-ACTION -
    % /push HEADING-ACTION -
    /heading-action {
        /replace eq {
             %
             % Before removing the tag at the top of the scope stack,
             % fill the end field of the tag.
             %
             % input0.man
             % ----------------------------------------------------
             %   .SH SEC1
             %   ...
             %E: This is the end of the SEC1.
             %   .SH
             %C: SEC2
             %
             % ----------------------------------------------------
             %
             % C represents the current input line. The parser must
             % fill the end field of "SEC1", the tag at the top,
             % with the line number of E.
             %
             %    E = C - 2
             %
             % input1.man
             % ----------------------------------------------------
             %   .SH SEC3
             %   ...
             %E: This is the end of the SEC1.
             %C:  .SH SEC4
             %
             % ----------------------------------------------------
             %
             % In this case
             %
             %    E = C - 1
             %
             % The offset for the adjustment depends on the conctxt.
             %
             fill-end-of-scope
             _scopepop
        } if

        _scopetop {
            . exch scope:
        } if
        . _scopepush
    } def
}}

--_tabledef-Man=EOF
--_tabledef-Man=SKIP
--_tabledef-Man=REST
--_tabledef-Man=GUARD

#
# helper tables
#
--_mtable-regex-Man=GUARD/([^\n.]|\.[^\nst])[^\n]*\n//{icase}
--_mtable-regex-Man=SKIP/[^\n]*\n|[^\n]+//
--_mtable-regex-Man=EOF///{scope=clear}{tquit}
--_mtable-extend-Man=REST+SKIP
--_mtable-extend-Man=REST+EOF

#
# main table
#
--_mtable-extend-Man=main+GUARD
--_mtable-regex-Man=main/\.TH[\t ]+"([^"]+)"[^\n]*\n/\1/t/{icase}{scope=set}
--_mtable-regex-Man=main/\.TH[\t ]+([^\t \n]+)[^\n]*\n/\1/t/{icase}{scope=set}
--_mtable-regex-Man=main/\.SH[\t ]+"([^"\n]+)"[^\n]*\n/\1/s/{icase}{scope=push}{tenter=section}
--_mtable-regex-Man=main/\.SH[\t ]+([^\n]+)\n/\1/s/{icase}{scope=push}{tenter=section}
--_mtable-regex-Man=main/\.SH[\t ]*\n//{icase}{tenter=sectionheading}{{
    /push
}}
--_mtable-extend-Man=main+REST

#
# section table
#
--_mtable-extend-Man=section+GUARD
--_mtable-regex-Man=section/\.SH[\t ]+"([^"\n]+)"[^\n]*\n/\1/s/{icase}{scope=replace}
--_mtable-regex-Man=section/\.SH[\t ]+([^\n]+)\n/\1/s/{icase}{scope=replace}
--_mtable-regex-Man=section/(\.SH)[\t ]*\n//{icase}{tjump=sectionheading}{{
    2 /replace
}}
--_mtable-regex-Man=section/\.SS[\t ]+"([^"\n]+)"[^\n]*\n/\1/S/{icase}{scope=push}{tenter=subsection}
--_mtable-regex-Man=section/\.SS[\t ]+([^\n]+)\n/\1/S/{icase}{scope=push}{tenter=subsection}
--_mtable-regex-Man=section/\.SS[\t ]*\n//{icase}{tenter=subsectionheading}{{
    /push
}}
--_mtable-extend-Man=section+REST

#
# sectionheading
#
--_mtable-regex-Man=sectionheading/[ \t]*([^\n]+)\n/\1/s/{tjump=section}{{
    heading-action
}}
--_mtable-extend-Man=sectionheading+REST

#
# subsection
#
--_mtable-extend-Man=subsection+GUARD
--_mtable-regex-Man=subsection/\.SS[\t ]+"([^"\n]+)"[^\n]*\n/\1/S/{icase}{scope=replace}
--_mtable-regex-Man=subsection/\.SS[\t ]+([^\n]+)\n/\1/S/{icase}{scope=replace}
--_mtable-regex-Man=subsection/\.SS[\t ]*\n//{icase}{tjump=subsectionheading}{{
    2 /replace
}}
--_mtable-regex-Man=subsection/(\.SH)///{icase}{_advanceTo=0start}{tleave}{{
    1 fill-end-of-scope
    _scopepop
}}
--_mtable-extend-Man=subsection+REST

#
# subsectionheading
#
--_mtable-regex-Man=subsectionheading/[ \t]*([^\n]+)\n/\1/S/{tjump=subsection}{{
    heading-action
}}
--_mtable-extend-Man=subsectionheading+REST
